您的位置:首页 > 编程语言 > Python开发

regular expression matching --python

2017-11-10 21:20 411 查看

递归

class Solution(object):
def isMatch(self, s, p):
if not p:
return not s
if len(p) == 1 or p[1] != '*':
return len(s) >0 and (p[0] == '.' or s[0] == p[0])and self.isMatch(s[1:], p[1:])
while s and (p[0] == '.' or s[0] == p[0]):
if self.isMatch(s, p[2:]):
return True
s = s[1:]
return self.isMatch(s, p[2:])


具体理解参考https://www.cnblogs.com/skysand/p/4292973.html

python自带正则表达式

def isMatch(self, s, p):
return re.match('^' + p + '$', s) != None


这应该是网上的两个最简洁的解题思路了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode