-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisMatch.py
More file actions
59 lines (53 loc) · 1.67 KB
/
isMatch.py
File metadata and controls
59 lines (53 loc) · 1.67 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File : isMatch.py
@Contact : 9824373@qq.com
@License : (C)Copyright 2017-2018, Zhan
@Desc :
@Modify Time @Author @Version @Desciption
------------ ------- -------- -----------
2019/5/31 22:19 zhan 1.0 None
'''
class Solution(object):
def isMatch(self, s, p):
"""
:type s: str
:type p: str
:rtype: bool
"""
dp = [{} for i in range(len(s) + 1)]
dp[0]['0'] = True
for i in range(1, len(s)+1):
dp[i]['0'] = False
for j in range(1, len(p)+1):
if j % 2 == 0:
dp[0][str(j)] = dp[0][str(j-2)] and p[j-1] == '*'
else:
dp[0][str(j)] = False
for i in range(1,len(s) + 1):
for j in range(1,len(p) + 1):
if p[j - 1] == '*':
dp[i][str(j)] = (dp[i].setdefault(str(j - 2),False) or (dp[i - 1].setdefault(str(j),False) and ((s[i - 1] == p[j - 2]) or p[j-2] == '.')))
elif p[j - 1] == '.':
dp[i][str(j)] = dp[i - 1].setdefault(str(j - 1), False)
else:
dp[i][str(j)] = (s[i - 1] == p[j - 1]
and dp[i - 1].setdefault(str(j - 1), False))
return dp[len(s)].setdefault(str(len(p)),False)
if __name__ == '__main__':
s = "ab"
p = ".*c"
# s = "aa"
# p = "a"
s = ""
p = ".*"
s = "aa"
p = "a*"
# s = "ab"
# p = ".*"
# s = "aab"
# p = "c*a*b"
s = "aab"
p = "b.*"
print(Solution().isMatch(s, p))