https://leetcode.com/problems/regular-expression-matching/#/description
class Solution(object):
def isMatch(self, s, p):
"""
:type s: str
:type p: str
:rtype: bool
"""
spos=0
ppos=0
while ppos<len(p) and spos<len(s):
if ppos<len(p)-1 and p[ppos+1]=='*':
while spos<len(s):
if self.isMatch(s[spos:], p[ppos+2:]):
return True
if p[ppos]=='.' or s[spos]==p[ppos]:
spos+=1
else:
return False
ppos+=2
break
if p[ppos]!='.' and p[ppos]!=s[spos]:
return False
ppos+=1
spos+=1
if ppos<len(p)-1 and p[ppos+1]=='*':
return self.isMatch(s[spos:], p[ppos+2:])
if spos<len(s) or ppos<len(p): return False
return True
思路:关键采用递归方式,处理*号,python字符串匹配到末端判断方式为读取位置与字符长度相同,因此代码有些冗余,没有c的漂亮