题解 | 字符串通配符

字符串通配符

https://www.nowcoder.com/practice/43072d50a6eb44d2a6c816a283b02036

  1. 递归法,注意处理"*"问题
  2. re匹配
a = input().lower()
b = input().lower()

def is_alnum(c):
    return ord("a") <= ord(c) <= ord("z") or ord("0") <= ord(c) <= ord("9")

def fun(s1, s2):
    if s1 == "" and s2 == "":
        return True
    if s1.replace("*", "") != "" and s2 == "":
        return False
    if s1 == "" and s2 != "":
        return False
    if s1[-1] == "?" and is_alnum(s2[-1]):
        return fun(s1[:-1], s2[:-1])
    elif s1[-1] == "*":
        return fun(s1[:-1], s2) or fun(s1, s2[:-1])
    elif s1[-1] == s2[-1]:
        return fun(s1[:-1], s2[:-1])
    else:
        return False

while "**" in a:
    a = a.replace("**", "*")
print("true" if fun(a, b) else "false")

# import re

# a = input().lower()
# b = input().lower()

# while "**" in a:
#     a = a.replace("**", "*")
# a = a.replace('.', r'\.').replace('?', '[a-z0-9]{1}').replace('*', '[a-z0-9]*')
# a = "^" + a + "$"

# if re.match(a, b):
#     print('true')
# else:
#     print('false')

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务