-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path明略科技1.py
32 lines (30 loc) · 962 Bytes
/
明略科技1.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
# -*- coding:utf-8 -*-
class Solution:
# s, patternattern都是字符串
def match(self, s, pattern):
# write code here
m, n = len(s), len(pattern)
if m ==0 or n==0:
return False
def matches(i, j):
if i == 0:
return False
if pattern[j - 1] == ".":
return True
return s[i - 1] == pattern[j - 1]
f = [[False] * (n + 1) for _ in range(n + 1)]
f[0][0] = True
for i in range(m + 1):
for j in range(1, n + 1):
if pattern[j - 1] == "*":
f[i][j] |= f[i][j - 2]
if matches(i, j - 1):
f[i][j] |= f[i - 1][j]
else:
if matches(i, j):
f[i][j] |= f[i - 1][j - 1]
return f[m][n]
if __name__ == '__main__':
s = "aaa"
p = "ab*ac*a"
print(Solution().match(s, p))