请实现一个函数用来匹配包括’.’和’*’的正则表达式。模式中的字符’.’表示任意一个字符,而’*’表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串”aaa”与模式”a.a”和”ab*ac*a”匹配,但是与”aa.a”和”ab*a”均不匹配
基本思路:递归,根据模式中第二个字符是不是”*”分两种情况讨论。
如果模式中第二个字符不是”*”:
1.如果字符串中第一个字符和模式中第一个字符相匹配,那么字符串和模式都后移一位。
2.如果字符串中第一个字符和模式中第一个字符不匹配,那么返回False。
如果模式中第二个字符是”*”:
1.如果字符串中第一个字符和模式中第一个字符相匹配:
(1)匹配一次,字符串后移一位,模式后移两位。
(2)匹配多次,字符串后移一位,模式不变。
(3)忽略,字符串不变,模式后移两位。
2.如果字符串中第一个字符和模式中第一个字符不匹配,那么模式后移两位。
# -*- coding:utf-8 -*-
class Solution:
# s, pattern都是字符串
def matchCore(self, s, sIndex, pattern, pIndex):
if sIndex == len(s) and pIndex == len(pattern):
return True
if sIndex != len(s) and pIndex == len(pattern):
return False
if pIndex + 1 < len(pattern) and pattern[pIndex + 1] == '*':
if sIndex < len(s) and (s[sIndex] == pattern[pIndex] or pattern[pIndex] == '.'):
return self.matchCore(s, sIndex + 1, pattern, pIndex + 2) or self.matchCore(s, sIndex + 1, pattern, pIndex) or self.matchCore(s, sIndex, pattern, pIndex + 2)
else:
return self.matchCore(s, sIndex, pattern, pIndex + 2)
if sIndex < len(s) and (s[sIndex] == pattern[pIndex] or pattern[pIndex] == '.'):
return self.matchCore(s, sIndex + 1, pattern, pIndex + 1)
return False
def match(self, s, pattern):
# write code here
sIndex, pIndex = 0, 0
return self.matchCore(s, sIndex, pattern, pIndex)
注意要时刻考虑数组是否越界。
升级版:动态规划。
# -*- coding:utf-8 -*-
class Solution:
# s, pattern都是字符串
def match(self, s, pattern):
# write code here
dp = [[False] * (len(pattern) + 1) for _ in range(len(s) + 1)]
dp[-1][-1] = True
for i in range(len(s), -1, -1):
for j in range(len(pattern) - 1, -1, -1):
first_match = i < len(s) and pattern[j] in {s[i], '.'}
if j + 1 < len(pattern) and pattern[j + 1] == '*':
dp[i][j] = dp[i][j + 2] or first_match and dp[i + 1][j]# or first_match and dp[i + 1][j + 2]
else:
dp[i][j] = first_match and dp[i + 1][j + 1]
return dp[0][0]