方法:DP
本题难度低于leetcode 10, 二十分钟做完,debug几个边界条件问题。代码运行时间较长1s python,应该有优于dp的算法。
class Solution:
def isMatch(self, s: str, p: str) -> bool:
dp = [[False for j in range(len(p) + 1)] for i in range(len(s) + 1)]
dp[0][0] = True
for j in range(1, len(p) + 1):
if p[j-1] == "*" and dp[0][j-1]:
dp[0][j] = True
for i in range(1, len(s) + 1):
for j in range(1, len(p) + 1):
if p[j-1] == s[i-1] or p[j-1] == "?":
dp[i][j] = dp[i-1][j-1]
elif p[j-1] == "*":
dp[i][j] = dp[i-1][j] or dp[i][j-1] or dp[i-1][j-1]
return dp[-1][-1]