簡易版正則表達式匹配,只有兩種通配符,”.”表示任意一個字符,”c*”表示字符c可以有零個或多個。
注意點:
存在一些不合理的字符組合,如”**” “.*”可以表示任意字符串 需要匹配整個目標串,而不是部分例子:
輸入: s=”aab”, p=”c*a*b”
輸出: True
開始用遞歸實現了一遍,結果超時,改用動態規劃。dp[i][j]表示s[i:]和p[j:]的匹配情況,圍繞”*”進行分類討論,分類的情況比較多,具體請參看代碼注釋。
class Solution(object):
def isMatch(self, s, p):
"""
:type s: str
:type p: str
:rtype: bool
"""
m = len(s)
n = len(p)
# Init dp
dp = [[False for i in range(n + 1)] for i in range(m + 1)]
# When string and pattern are all None
dp[m][n] = True
# When the string is None, pattern like "a*" can still match it
for i in range(n - 1, -1, -1):
if p[i] == "*":
dp[m][i] = dp[m][i + 1]
elif i + 1 < n and p[i + 1] == "*":
dp[m][i] = dp[m][i + 1]
else:
dp[m][i] = False
for i in range(m - 1, -1, -1):
for j in range(n - 1, -1, -1):
# When the current character is "*"
if p[j] == "*":
if j - 1 >= 0 and p[j - 1] != "*":
dp[i][j] = dp[i][j + 1]
# If the pattern is starting with "*" or has "**" in it
else:
return False
# When the the second character of pattern is "*"
elif j + 1 < n and p[j + 1] == "*":
# When the current character matches, there are three possible situation
# 1. ".*" matches nothing
# 2. "c*" matches more than one character
# 3. "c*" just matches one character
if s[i] == p[j] or p[j] == ".":
dp[i][j] = dp[i][j + 2] or dp[i + 1][j] or dp[i + 1][j + 2]
# Ignore the first two characters("c*") in pattern since they cannot match
# the current character in string
else:
dp[i][j] = dp[i][j + 2]
else:
# When the current character is matched
if s[i] == p[j] or p[j] == ".":
dp[i][j] = dp[i + 1][j + 1]
else:
dp[i][j] = False
return dp[0][0]
if __name__ == "__main__":
assert Solution().isMatch("aa", "a") == False
assert Solution().isMatch("aa", "aa") == True
assert Solution().isMatch("aaa", "aa") == False
assert Solution().isMatch("aa", "a*") == True
assert Solution().isMatch("aa", ".*") == True
assert Solution().isMatch("ab", ".*") == True
assert Solution().isMatch("aab", "c*a*b") == True