Description
- Regular Expression Matching Hard
Given an input string (s) and a pattern §, implement regular expression matching with support for ‘.’ and ‘*’.
'.' Matches any single character.
'*' Matches zero or more of the preceding element.
The matching should cover the entire input string (not partial).
Note:
- s could be empty and contains only lowercase letters a-z.
- p could be empty and contains only lowercase letters a-z, and characters like .
or *.
Example
- Example 1:
Input:
s = "aa"
p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".
- Example 2:
Input:
s = "aa"
p = "a*"
Output: true
Explanation: '*' means zero or more of the precedeng element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
- Example 3:
Input:
s = "ab"
p = ".*"
Output: true
Explanation: ".*" means "zero or more (*) of any character (.)".
- Example 4:
Input:
s = "aab"
p = "c*a*b"
Output: true
Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab".
- Example 5:
Input:
s = "mississippi"
p = "mis*is*p*."
Output: false
题意
给定输入字符串s和模式p,实现支持’.‘和’*‘的正则表达式匹配。
‘.’ 匹配任何单个字符,’ * '匹配前面元素的零个或多个。匹配覆盖整个输入字符串而不是部分。
(跟python中的正则表达式规则一样)
思路1 re.match
其实这题我最开始的想法竟然是分类讨论,就是看’.‘和’*'分别在不在模式p中,后来发现太多细节要考虑了,最后不了了之。后面参考思路一看,这不就是python中正则表达式的特性吗,直接用re模块的match方法解决便可。
code
class Solution:
def isMatch(self, s: str, p: str) -> bool:
return re.match('^' + p + '$',s) != None
【笔记】
re.match(pattern, string[, flags])
从首字母开始开始匹配,string如果包含pattern子串,则匹配成功,返回Match对象,失败则返回None,若要完全匹配,pattern要以$结尾。
^+$分别表示:
1、^:匹配输入字符串的开始位置。
2、+:匹配前面的子表达式一次或多次(大于等于1次)。
3、$:匹配输入字符串的结束位置。
思路2 递归
递归思路大致如下:
-
若p为空,若s也为空,返回true,否则返回false。
-
若p的长度为1,若s长度也为1,且相同或p为’.’,则返回true,否则返回false。
-
若p的第二个字符为*,进行下列循环,若s不为空且首字符匹配(包括p[0]为’.’),调用递归函数匹配s和去掉前两个字符的p(这样做的原因是假设此时的星号的作用是让前面的字符出现0次,验证是否匹配),若匹配返回true;否则s去掉首字母(因为此时首字母匹配了,我们可以去掉s的首字母,而p由于星号的作用,可以有任意个首字母,所以不需要去掉),继续进行循环。
-
若p的第二个字符不为*,若此时s为空返回false,否则判断首字符是否匹配,且从各自的第二个字符开始调用递归函数匹配。
code
class Solution:
def isMatch(self, s: str, p: str) -> bool:
if not p:
return not s
first_match = bool(s) and p[0] in {s[0], '.'}
if len(p) >=2 and p[1] == '*':
return first_match and self.isMatch(s[1:], p) or self.isMatch(s,p[2:])
else:
return first_match and self.isMatch(s[1:], p[1:])
递归的时间复杂度很大,faster than 15.17%
思路3 动态规划
用DP来解,定义一个二维的DP数组,其中dp[i][j]表示s[0,i)和p[0,j)是否match,然后有下面三种情况:
- P[i][j] = P[i - 1][j - 1], if p[j - 1] != ‘*’ && (s[i - 1] == p[j - 1] || p[j - 1] == ‘.’);
- P[i][j] = P[i][j - 2], if p[j - 1] == ‘*’ and the pattern repeats for 0 times;
- P[i][j] = P[i - 1][j] && (s[i - 1] == p[j - 2] || p[j - 2] == ‘.’), if p[j - 1] == ‘*’ and the pattern repeats for at least 1 times.
code
class Solution:
def isMatch(self, s: str, p: str) -> bool:
dp=[[False for i in range(len(p)+1)] for j in range(len(s)+1)]
dp[0][0]= True
for j in range(1, len(p)+1):
if p[j-1] == '*':
if j >= 2:
dp[0][j] = dp[0][j-2]
for i in range(1, len(s)+1):
for j in range(1, len(p)+1):
if p[j-1] != '*' and (s[i-1] == p[j-1] or p[j-1] == '.'):
dp[i][j] = dp[i-1][j-1]
elif p[j-1] == '*':
dp[i][j] =dp[i-1][j] and (s[i-1] == p[j-2] or p[j-2] == '.') or dp[i][j-2]
return dp[len(s)][len(p)]
注意: 当p[j-1] == '*'时还是要考虑dp[i][j-2]的情况。
又周五了,一周过好快。明天去刷英语6级分数啦。