-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
substring-matching-pattern.py
44 lines (40 loc) · 1.13 KB
/
substring-matching-pattern.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
# Time: O(n + m)
# Space: O(m)
# kmp
class Solution(object):
def hasMatch(self, s, p):
"""
:type s: str
:type p: str
:rtype: bool
"""
def getPrefix(pattern):
prefix = [-1]*len(pattern)
j = -1
for i in xrange(1, len(pattern)):
while j+1 > 0 and pattern[j+1] != pattern[i]:
j = prefix[j]
if pattern[j+1] == pattern[i]:
j += 1
prefix[i] = j
return prefix
def KMP(text, pattern, i):
prefix = getPrefix(pattern)
j = -1
for i in xrange(i, len(text)):
while j+1 > 0 and pattern[j+1] != text[i]:
j = prefix[j]
if pattern[j+1] == text[i]:
j += 1
if j+1 == len(pattern):
return i-j
return -1
i = 0
for x in p.split('*'):
if not x:
continue
i = KMP(s, x, i)
if i == -1:
return False
i += len(x)
return True