题解 | #字符串通配符#
字符串通配符
https://www.nowcoder.com/practice/43072d50a6eb44d2a6c816a283b02036
s1, s2 = input().lower(), input().lower()
isMatch = [[False] * (len(s2) + 1)]
isMatch[0][0] = True
for i in range(1, len(s1) + 1):
isMatch.append([False] * (len(s2) + 1))
if s1[i - 1] == '*':
isMatch[i][0] = isMatch[i - 1][0]
for j in range(1, len(s2) + 1):
if s1[i - 1] == s2[j - 1] or (s1[i - 1] == '?' and (s2[j - 1].isdigit() or s2[j - 1].isalpha())):
isMatch[i][j] = isMatch[i - 1][j - 1]
elif s1[i - 1] == '*':
isMatch[i][j] = isMatch[i - 1][j] or (isMatch[i][j - 1] and (s2[j - 1].isdigit() or s2[j - 1].isalpha()))
if isMatch[-1][-1]:
print('true')
else:
print('false')
动态规划思路
