-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy path44.通配符匹配.py
39 lines (38 loc) · 1.14 KB
/
44.通配符匹配.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
# 给定一个字符串 (s) 和一个字符模式 (p) ,实现一个支持 '?' 和 '*' 的通配符匹配。
#
# '?' 可以匹配任何单个字符。
# '*' 可以匹配任意字符串(包括空字符串)。
# 两个字符串完全匹配才算匹配成功。
#
# 说明:
#
# s 可能为空,且只包含从 a-z 的小写字母。
# p 可能为空,且只包含从 a-z 的小写字母,以及字符 ? 和 *。
# 示例 1:
#
# 输入:
# s = "aa"
# p = "a"
# 输出: false
# 解释: "a" 无法匹配 "aa" 整个字符串。
class Solution:
def isMatch(self, s: str, p: str) -> bool:
i = 0
j = 0
start = -1
match = 0
while i < len(s):
if j < len(p) and (s[i] == p[j] or p[j] == "?"):
i += 1
j += 1
elif j < len(p) and p[j] == "*":
start = j
match = i
j += 1
elif start != -1:
j = start + 1
match += 1
i = match
else:
return False
return all(x=="*" for x in p[j:])