Skip to content

Commit a920c3b

Browse files
committed
feat: add solutions to lc problem: No.0028
No.0028.Implement strStr()
1 parent 1e2f890 commit a920c3b

File tree

3 files changed

+176
-8
lines changed

3 files changed

+176
-8
lines changed

solution/0000-0099/0028.Implement strStr()/README.md

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private:
125125
vector<int> Next(string str)
126126
{
127127
vector<int> n(str.length()) ;
128-
n[0] = -1 ;
128+
n[0] = -1 ;
129129
int i = 0, pre = -1 ;
130130
int len = str.length() ;
131131
while (i < len)
@@ -146,9 +146,9 @@ public:
146146
int strStr(string haystack, string needle) {
147147
if (0 == needle.length())
148148
return 0 ;
149-
149+
150150
vector<int> n(Next(needle)) ;
151-
151+
152152
int len = haystack.length() - needle.length() + 1 ;
153153
for (int i = 0; i < len; ++i)
154154
{
@@ -170,7 +170,7 @@ public:
170170
if (j >= needle.length())
171171
return k-j ;
172172
}
173-
173+
174174
return -1 ;
175175
}
176176
};
@@ -275,6 +275,74 @@ function strStr(haystack: string, needle: string): number {
275275
}
276276
```
277277

278+
```ts
279+
function strStr(haystack: string, needle: string): number {
280+
const m = haystack.length;
281+
const n = needle.length;
282+
const next = new Array(n).fill(0);
283+
let j = 0;
284+
for (let i = 1; i < n; i++) {
285+
while (j > 0 && needle[i] !== needle[j]) {
286+
j = next[j - 1];
287+
}
288+
if (needle[i] === needle[j]) {
289+
j++;
290+
}
291+
next[i] = j;
292+
}
293+
j = 0;
294+
for (let i = 0; i < m; i++) {
295+
while (j > 0 && haystack[i] !== needle[j]) {
296+
j = next[j - 1];
297+
}
298+
if (haystack[i] === needle[j]) {
299+
j++;
300+
}
301+
if (j === n) {
302+
return i - n + 1;
303+
}
304+
}
305+
return -1;
306+
}
307+
```
308+
309+
### **Rust**
310+
311+
```rust
312+
impl Solution {
313+
pub fn str_str(haystack: String, needle: String) -> i32 {
314+
let haystack = haystack.as_bytes();
315+
let needle = needle.as_bytes();
316+
let m = haystack.len();
317+
let n = needle.len();
318+
let mut next = vec![0; n];
319+
let mut j = 0;
320+
for i in 1..n {
321+
while j > 0 && needle[i] != needle[j] {
322+
j = next[j - 1];
323+
}
324+
if needle[i] == needle[j] {
325+
j += 1;
326+
}
327+
next[i] = j;
328+
}
329+
j = 0;
330+
for i in 0..m {
331+
while j > 0 && haystack[i] != needle[j] {
332+
j = next[j - 1];
333+
}
334+
if haystack[i] == needle[j] {
335+
j += 1;
336+
}
337+
if j == n {
338+
return (i - n + 1) as i32;
339+
}
340+
}
341+
-1
342+
}
343+
}
344+
```
345+
278346
### **...**
279347

280348
```

solution/0000-0099/0028.Implement strStr()/README_EN.md

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private:
106106
vector<int> Next(string str)
107107
{
108108
vector<int> n(str.length()) ;
109-
n[0] = -1 ;
109+
n[0] = -1 ;
110110
int i = 0, pre = -1 ;
111111
int len = str.length() ;
112112
while (i < len)
@@ -127,9 +127,9 @@ public:
127127
int strStr(string haystack, string needle) {
128128
if (0 == needle.length())
129129
return 0 ;
130-
130+
131131
vector<int> n(Next(needle)) ;
132-
132+
133133
int len = haystack.length() - needle.length() + 1 ;
134134
for (int i = 0; i < len; ++i)
135135
{
@@ -151,7 +151,7 @@ public:
151151
if (j >= needle.length())
152152
return k-j ;
153153
}
154-
154+
155155
return -1 ;
156156
}
157157
};
@@ -256,6 +256,74 @@ function strStr(haystack: string, needle: string): number {
256256
}
257257
```
258258

259+
```ts
260+
function strStr(haystack: string, needle: string): number {
261+
const m = haystack.length;
262+
const n = needle.length;
263+
const next = new Array(n).fill(0);
264+
let j = 0;
265+
for (let i = 1; i < n; i++) {
266+
while (j > 0 && needle[i] !== needle[j]) {
267+
j = next[j - 1];
268+
}
269+
if (needle[i] === needle[j]) {
270+
j++;
271+
}
272+
next[i] = j;
273+
}
274+
j = 0;
275+
for (let i = 0; i < m; i++) {
276+
while (j > 0 && haystack[i] !== needle[j]) {
277+
j = next[j - 1];
278+
}
279+
if (haystack[i] === needle[j]) {
280+
j++;
281+
}
282+
if (j === n) {
283+
return i - n + 1;
284+
}
285+
}
286+
return -1;
287+
}
288+
```
289+
290+
### **Rust**
291+
292+
```rust
293+
impl Solution {
294+
pub fn str_str(haystack: String, needle: String) -> i32 {
295+
let haystack = haystack.as_bytes();
296+
let needle = needle.as_bytes();
297+
let m = haystack.len();
298+
let n = needle.len();
299+
let mut next = vec![0; n];
300+
let mut j = 0;
301+
for i in 1..n {
302+
while j > 0 && needle[i] != needle[j] {
303+
j = next[j - 1];
304+
}
305+
if needle[i] == needle[j] {
306+
j += 1;
307+
}
308+
next[i] = j;
309+
}
310+
j = 0;
311+
for i in 0..m {
312+
while j > 0 && haystack[i] != needle[j] {
313+
j = next[j - 1];
314+
}
315+
if haystack[i] == needle[j] {
316+
j += 1;
317+
}
318+
if j == n {
319+
return (i - n + 1) as i32;
320+
}
321+
}
322+
-1
323+
}
324+
}
325+
```
326+
259327
### **...**
260328

261329
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
impl Solution {
2+
pub fn str_str(haystack: String, needle: String) -> i32 {
3+
let haystack = haystack.as_bytes();
4+
let needle = needle.as_bytes();
5+
let m = haystack.len();
6+
let n = needle.len();
7+
let mut next = vec![0; n];
8+
let mut j = 0;
9+
for i in 1..n {
10+
while j > 0 && needle[i] != needle[j] {
11+
j = next[j - 1];
12+
}
13+
if needle[i] == needle[j] {
14+
j += 1;
15+
}
16+
next[i] = j;
17+
}
18+
j = 0;
19+
for i in 0..m {
20+
while j > 0 && haystack[i] != needle[j] {
21+
j = next[j - 1];
22+
}
23+
if haystack[i] == needle[j] {
24+
j += 1;
25+
}
26+
if j == n {
27+
return (i - n + 1) as i32;
28+
}
29+
}
30+
-1
31+
}
32+
}

0 commit comments

Comments
 (0)