Skip to content

Commit bc87f1e

Browse files
committed
solve problem Implement Strstr
1 parent da007c5 commit bc87f1e

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ All solutions will be accepted!
178178
|643|[Maximum Average Subarray I](https://leetcode-cn.com/problems/maximum-average-subarray-i/description/)|[java/py/js](./algorithms/MaximumAverageSubarrayI)|Easy|
179179
|189|[Rotate Array](https://leetcode-cn.com/problems/rotate-array/description/)|[java/py/js](./algorithms/RotateArray)|Easy|
180180
|687|[Longest Univalue Path](https://leetcode-cn.com/problems/longest-univalue-path/description/)|[java/py/js](./algorithms/LongestUnivaluePath)|Easy|
181+
|28|[Implement Strstr](https://leetcode-cn.com/problems/implement-strstr/description/)|[java/py/js](./algorithms/ImplementStrstr)|Easy|
181182

182183
# Database
183184
|#|Title|Solution|Difficulty|

algorithms/ImplementStrstr/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Implement Strstr
2+
For performance we can use KMP to solve this problem
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public int strStr(String haystack, String needle) {
3+
int needleLength = needle.length();
4+
if (needleLength == 0) return 0;
5+
6+
int i = 0, j = 0;
7+
List<Integer> next = new ArrayList<Integer>();
8+
9+
while (i < needleLength) {
10+
if (i == 0 || (needle.charAt(i) != needle.charAt(j) && j == 0)) {
11+
next.add(0);
12+
} else if (needle.charAt(i) == needle.charAt(j)) {
13+
next.add(j + 1);
14+
j++;
15+
} else if (needle.charAt(i) != needle.charAt(j)) {
16+
j = next.get(j - 1);
17+
continue;
18+
}
19+
i++;
20+
}
21+
22+
i = j = 0;
23+
24+
while (i < haystack.length() && j < needleLength) {
25+
if (haystack.charAt(i) == needle.charAt(j)) {
26+
i++;
27+
j++;
28+
} else if (j == 0) {
29+
i++;
30+
} else {
31+
j = next.get(j - 1);
32+
}
33+
}
34+
35+
return j == needleLength ? i - j : -1;
36+
}
37+
}
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @param {string} haystack
3+
* @param {string} needle
4+
* @return {number}
5+
*/
6+
var strStr = function(haystack, needle) {
7+
if (needle === '') return 0
8+
9+
let next = [],
10+
i = j = 0
11+
12+
while (i < needle.length) {
13+
if (i === 0 || (needle[i] !== needle[j] && j === 0)) {
14+
next.push(0)
15+
} else if (needle[i] === needle[j]) {
16+
next.push(j + 1)
17+
j++
18+
} else if (needle[i] !== needle[j]) {
19+
j = next[j - 1]
20+
continue
21+
}
22+
i++
23+
}
24+
i = j = 0
25+
26+
while (i < haystack.length && j < needle.length) {
27+
if (haystack[i] == needle[j]) {
28+
i++
29+
j++
30+
} else if (j === 0) {
31+
i++
32+
} else {
33+
j = next[j - 1]
34+
}
35+
}
36+
37+
return j === needle.length ? i - j : -1
38+
};
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution(object):
2+
def strStr(self, haystack, needle):
3+
"""
4+
:type haystack: str
5+
:type needle: str
6+
:rtype: int
7+
"""
8+
if needle == '':
9+
return 0
10+
11+
nxt = []
12+
i = j = 0
13+
14+
while i < len(needle):
15+
if i == 0 or (needle[i] != needle[j] and j == 0):
16+
nxt.append(0)
17+
elif needle[i] == needle[j]:
18+
nxt.append(j + 1)
19+
j += 1
20+
elif needle[i] != needle[j] and j > 0:
21+
j = nxt[j - 1]
22+
continue
23+
i += 1
24+
25+
i = j = 0
26+
27+
while i < len(haystack) and j < len(needle):
28+
if haystack[i] == needle[j]:
29+
i += 1
30+
j += 1
31+
elif j == 0:
32+
i += 1
33+
else:
34+
j = nxt[j - 1]
35+
36+
return i - j if j == len(needle) else -1

0 commit comments

Comments
 (0)