题解 | #字符串通配符#
字符串通配符
https://www.nowcoder.com/practice/43072d50a6eb44d2a6c816a283b02036
s1 = input() s2 = input() m = len(s1) n = len(s2) dp = [[False for j in range(n + 1)] for i in range(m + 1)] dp[0][0] = True # 处理第0行() for j in range(1, n + 1): dp[0][j] = False # 处理第0列(特别注意第0列的处理方式) for i in range(1, m + 1): if s1[i - 1] == "*": dp[i][0] = True else: break # 处理其他值 for i in range(1, m + 1): for j in range(1, n + 1): if s1[i - 1].lower() == s2[j - 1].lower(): # 注意这里一定统一大写或者统一小写,才好比较(不区分时会报错) dp[i][j] = dp[i - 1][j - 1] if s1[i - 1] == "?" and s2[j - 1].isalnum(): # isalnum()表示所有字符均为字母或者数字 dp[i][j] = dp[i - 1][j - 1] if s1[i - 1] == "*": dp[i][j] = dp[i - 1][j] or dp[i][j - 1] if dp[m][n]: print("true") else: print("false")