-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq44_Wildcard_Matching.py
141 lines (120 loc) · 5.7 KB
/
q44_Wildcard_Matching.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
class Solution:
def isMatch(self, s, p):
"""
:type s: str
:type p: str
:rtype: bool
"""
def match(s, p):
if p == '?':
return True
return p == s
dict = {}
for i in range(len(s) + 1):
for j in range(len(p) + 1):
if j == 0:
dict[i, j] = True if i == 0 else False
elif p[j - 1] == '*':
dict[i, j] = False
for x in range(i + 1):
if dict[x, j - 1]:
dict[i, j] = True
break
elif i == 0:
dict[i, j] = False
else:
dict[i, j] = dict[i - 1, j - 1] and match(s[i - 1], p[j - 1])
return dict[len(s), len(p)]
def isMatch1(self, s, p):
"""
:type s: str
:type p: str
:rtype: bool
"""
def match(s, p):
if p == '?':
return True
return p == s
dict = {}
ns = 0
for i in range(len(p)):
if p[i]=='*':
ns += 1
elif ns == 0:
if i>=len(s) or not match(s[i],p[i]):
return False
else:
dict[i+1,i+1]=True
if len(p)-ns>len(s):
return False
def dp(i, j):
if (i, j) not in dict:
if j == 0:
dict[i, j] = True if i == 0 else False
elif p[j - 1] == '*':
dict[i, j] = False
for x in range(i+1):
if dp(x, j - 1):
dict[i, j] = True
break
elif i == 0:
dict[i, j] = False
else:
dict[i, j] = match(s[i - 1], p[j - 1]) and dp(i - 1, j - 1)
return dict[i, j]
return dp(len(s), len(p))
def isMatch2(self, s, p):
if not p:
return not s
m, n = len(s), len(p)
i = j = 0
last_x = 0
last_y = -1
while i < m:
if j < n and (p[j] == '?' or p[j] == s[i]):
i += 1
j += 1
elif j < n and p[j] == '*':
last_x = i
last_y = j
j += 1
elif last_y >= 0:
i = last_x + 1
j = last_y
else:
return False
while j < n and p[j] == '*':
j += 1
return j == n
def isMatch3(self, s, p):
"""
:type s: str
:type p: str
:rtype: bool
"""
def match(s, p):
return p == s or p == '?'
dict = {}
def dp(i, j):
if (i, j) not in dict:
if j == 0:
dict[i, j] = True if i == 0 else False
elif p[j - 1] == '*':
if i>0:
dict[i, j] = dp(i-1,j) or dp(i,j-1)
else:
dict[i,j] = dp(i,j-1)
elif i == 0:
dict[i, j] = False
else:
dict[i, j] = match(s[i - 1], p[j - 1]) and dp(i - 1, j - 1)
return dict[i, j]
return dp(len(s), len(p))
# s="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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