forked from huxiaoman7/leetcodebook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10_RegularExpressionMatching.py
More file actions
45 lines (37 loc) · 1.17 KB
/
Copy path10_RegularExpressionMatching.py
File metadata and controls
45 lines (37 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/2/28 3:35 PM
# @Author : huxiaoman
# @File : 10_RegularExpressionMatching.py
# @Package : LeetCode
# @E-mail : charlotte77_hu@sina.com
import re
# 方法一:正则匹配
class Solution1(object):
def isMatch(self, s, p):
"""
:type s: str
:type p: str
:rtype: bool
"""
return re.match('^'+p+'$',s) != None
#方法二:dp
class Solution2(object):
def isMatch(self, s, p):
dp = [[False] * (len(s) + 1) for _ in range(len(p) + 1)]
print dp
dp[0][0] = True
for i in range(1, len(p)):
dp[i + 1][0] = dp[i - 1][0] and p[i] == '*'
for i in range(len(p)):
for j in range(len(s)):
if p[i] == '*':
dp[i + 1][j + 1] = dp[i - 1][j + 1] or dp[i][j + 1]
if p[i - 1] == s[j] or p[i - 1] == '.':
dp[i + 1][j + 1] |= dp[i + 1][j]
else:
dp[i + 1][j + 1] = dp[i][j] and (p[i] == s[j] or p[i] == '.')
return dp[-1][-1]
if __name__ == '__main__':
s = Solution2()
print s.isMatch('aa','a')