-
Notifications
You must be signed in to change notification settings - Fork 24
/
44 - Wildcard Matching.py
35 lines (35 loc) · 1.29 KB
/
44 - 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
# Solution 1: Dynamic programming with O(n*s) runtime and O(n*s) space
class Solution(object):
def isMatch(self, s, p):
"""
:type s: str
:type p: str
:rtype: bool
"""
# Edge case
if len(s) == 0:
for c in p:
if c != "*":
return False
return True
matrix = [[False for c in range(len(s)+1)] for r in range(len(p)+1)]
matrix[0][0] = True # can always match empty string
for i in range(1,len(p)+1):
for j in range(1,len(s)+1):
if p[i-1] == "?":
matrix[i][j] = matrix[i-1][j-1] # automatic match
elif p[i-1] == "*":
k = 0
while k < len(s)+1 and not matrix[i-1][k]:
k += 1
# If we found a match without the *, we can match the rest of the string
for l in range(k,len(s)+1):
matrix[i][l] = True
break
else:
# Letter is normal char
if p[i-1] == s[j-1] and matrix[i-1][j-1]:
matrix[i][j] = True
else:
matrix[i][j] = False
return matrix[-1][-1]