-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplement-strstr.py
More file actions
65 lines (55 loc) · 1.66 KB
/
implement-strstr.py
File metadata and controls
65 lines (55 loc) · 1.66 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# encoding: utf-8
__author__ = 'zhangwei'
class Solution:
# @param haystack, a string
# @param needle, a string
# @return an integer
def strStr(self, haystack, needle):
# haystack - 文本串,needle - 模式串
m, n = len(haystack), len(needle)
for i in xrange(0, m-n+1):
flag = True
for j in xrange(0, n):
if haystack[i+j] != needle[j]:
flag = False
break
if flag:
return i
return -1
# KMP算法
class Solution:
# @param haystack, a string
# @param needle, a string
# @return an integer
def strStr(self, haystack, needle):
# haystack - 文本串,needle - 模式串
m, n = len(haystack), len(needle)
# 计算next
next = self.calNext(needle)
i = j = 0
while i < m and j < n:
if haystack[i] == needle[j]:
i, j = i+1, j+1
else: # find k and update j
if next[j] == -1:
i, j = i+1, 0
else:
j = next[j]
if j == n:
return i-n
return -1
def calNext(self, pattern): # pattern - 模式串
n = len(pattern)
next = [-1] * n
j, k = 0, -1 # j, k = 0, next[0]
while j < n-1:
if k == -1 or pattern[k] == pattern[j]:
j, k = j+1, k+1
#next[j] = k
if pattern[j] == pattern[k]:
next[j] = next[k]
else:
next[j] = k
else:
k = next[k]
return next