-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisMatch.py
More file actions
42 lines (42 loc) · 795 Bytes
/
isMatch.py
File metadata and controls
42 lines (42 loc) · 795 Bytes
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
class Solution(object):
def isMatch(self, s, p):
"""
:type s: str
:type p: str
:rtype: bool
"""
lp = len(p)
i = 0
if lp == 0:
return len(s) == 0
ls = len(s)
j = 0
star = False
starpos = -1
while j<ls:
if (i<lp and j<ls) and (p[i] == s[j] or p[i] == "?"):
i+=1
j+=1
else:
if i<lp and p[i] == "*":
starpos = i
jIndex = j
star = True
i+=1
elif star:
j = jIndex +1
i = starpos+1
jIndex +=1
else:
return False
while i<lp and p[i] == "*":
i+=1
return i==lp
s = Solution()
assert(s.isMatch("aa","a")==False)
assert(s.isMatch("aa","aa"))
assert(s.isMatch("aaaa", "*"))
assert(s.isMatch("aa", "a*"))
assert(s.isMatch("ab", "*?"))
assert(s.isMatch("aab", "c*a*b")==False)
assert(s.isMatch("c", "*?*"))