Skip to content

Commit 9589403

Browse files
committed
Update README.md
Add KMP for Problem 28.
1 parent 835e147 commit 9589403

File tree

2 files changed

+65
-25
lines changed

2 files changed

+65
-25
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
# My Python Solution for Leetcode
1+
# My Python Solutions for Leetcode
22

3+
[The whole lists](https://github.com/qiyuangong/leetcode/tree/master/python):
34

45
| # | Title | Solution | Basic idea (One line) |
56
|---| ----- | -------- | ---------- |
6-
| 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1_Two_Sum.py) | 1. hash 2. Sort and search with two point|
7-
| 15 | [3Sum](https://leetcode.com/problems/3sum/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/15_3Sum.py) | 1. Sort and O(n) search with three points 2. Multiple Two Sum (Problem 1) |
7+
| 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1_Two_Sum.py) | 1. hash 2. Sort and search with two points|
8+
| 15 | [3Sum](https://leetcode.com/problems/3sum/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/15_3Sum.py) | 1. Sort and O(n^2) search with three points 2. Multiple Two Sum (Problem 1) |
9+
| 16 | [3Sum Closest](https://leetcode.com/problems/3sum-closest/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/16_3Sum_Closest.py) | Sort and Multiple Two Sum check abs|
10+
| 18 | [4Sum](https://leetcode.com/problems/4sum/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/18_4Sum.py) | The same as 3Sum, but we can merge pairs with the same sum |
11+
| 28 | [Implement strStr()](https://leetcode.com/problems/implement-strstr/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/28_Implement_strStr().py) | 1. O(nm) comparison 2. KMP|
12+
| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/125_Valid_Palindrome.py)| Exclude non-alphanumeric characters and compare O(n) |
13+
||

python/28_Implement_strStr().py

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
class Solution(object):
2-
def strStr(self, haystack, needle):
3-
"""
4-
:type haystack: str
5-
:type needle: str
6-
:rtype: int
7-
"""
8-
lsh, lsn = len(haystack), len(needle)
9-
if lsn == 0:
10-
return 0
11-
pos, index = 0, 0
12-
while index <= lsh - lsn:
13-
if haystack[index] == needle[pos]:
14-
backup = index
15-
while index < lsh and pos < lsn and haystack[index] == needle[pos]:
16-
pos += 1
17-
index += 1
18-
if pos == len(needle):
19-
return index - pos
20-
index = backup
21-
index += 1
22-
pos = 0
23-
return -1
2+
# def strStr(self, haystack, needle):
3+
# """
4+
# :type haystack: str
5+
# :type needle: str
6+
# :rtype: int
7+
# """
8+
# lsh, lsn = len(haystack), len(needle)
9+
# if lsn == 0:
10+
# return 0
11+
# pos, index = 0, 0
12+
# while index <= lsh - lsn:
13+
# if haystack[index] == needle[pos]:
14+
# backup = index
15+
# while index < lsh and pos < lsn and haystack[index] == needle[pos]:
16+
# pos += 1
17+
# index += 1
18+
# if pos == len(needle):
19+
# return index - pos
20+
# index = backup
21+
# index += 1
22+
# pos = 0
23+
# return -1
2424

2525
# def strStr(self, haystack, needle):
2626
# lsh, lsn = len(haystack), len(needle)
@@ -34,3 +34,37 @@ def strStr(self, haystack, needle):
3434
# return index
3535
# index += 1
3636
# return -1
37+
38+
# KMP
39+
# https://discuss.leetcode.com/topic/3576/accepted-kmp-solution-in-java-for-reference/2
40+
def strStr(self, haystack, needle):
41+
lsh, lsn = len(haystack), len(needle)
42+
if lsn == 0:
43+
return 0
44+
next = self.makeNext(needle)
45+
i = j = 0
46+
while i < lsh:
47+
if j == -1 or haystack[i] == needle[j]:
48+
i += 1
49+
j += 1
50+
if j == lsn:
51+
return i - lsn
52+
if i < lsh and haystack[i] != needle[j]:
53+
j = next[j]
54+
return -1
55+
56+
def makeNext(self, needle):
57+
ls = len(needle)
58+
next = [0] * ls
59+
next[0], i, j = -1, 0, -1
60+
while i < ls - 1:
61+
if j == -1 or needle[i] == needle[j]:
62+
next[i + 1] = j + 1
63+
if needle[i + 1] == needle[j + 1]:
64+
next[i + 1] = next[j + 1]
65+
i += 1
66+
j += 1
67+
if needle[i] != needle[j]:
68+
j = next[j]
69+
return next
70+

0 commit comments

Comments
 (0)