forked from y-ncao/Python-Study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWildcard_Matching.py
51 lines (45 loc) · 1.63 KB
/
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
"""
Implement wildcard pattern matching with support for '?' and '*'.
'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence).
The matching should cover the entire input string (not partial).
The function prototype should be:
bool isMatch(const char *s, const char *p)
Some examples:
isMatch("aa","a") -> false
isMatch("aa","aa") -> true
isMatch("aaa","aa") -> false
isMatch("aa", "*") -> true
isMatch("aa", "a*") -> true
isMatch("ab", "?*") -> true
isMatch("aab", "c*a*b") -> false
"""
class Solution:
# @param s, an input string
# @param p, a pattern string
# @return a boolean
def isMatch(self, s, p):
i = 0
j = 0
backupS = -1
backupP = -1
while i < len(s):
if j < len(p) and (p[j] == '?' or s[i] == p[j]): # Move to next if s[i] == p[j] or p[j] == '?'
i += 1
j += 1
elif j < len(p) and p[j] == '*': # Backup if p[j] == '*'. Keep s but move p
j += 1
backupS = i
backupP = j
else: # No match
if backupP == -1: # if no backup, return false
return False
backupS += 1 # Have a backup, move backupS, restore all the backup
i = backupS
j = backupP
while j < len(p) and p[j] == '*':
j += 1
return j == len(p) # and i == len(s)
# Note
# 1. Line 47 can be removed because when it's out of loop, i must == len(s)
# 2. Line 39 doens't matter if it is backupS or backupP