@@ -47,13 +47,8 @@ The first occurrence is at index 0, so we return 0.
47
47
class Solution :
48
48
def strStr (self ,haystack :str ,needle :str ) ->int :
49
49
for iin range (len (haystack)):
50
- j= 0
51
-
52
- while i+ j< len (haystack)and haystack[i+ j]== needle[j]:
53
- j+= 1
54
-
55
- if j== len (needle):
56
- return i
50
+ if haystack[i:i+ len (needle)]== needle:
51
+ return i
57
52
58
53
return - 1
59
54
```
@@ -62,14 +57,8 @@ class Solution:
62
57
``` javascript
63
58
var strStr = function (haystack ,needle ) {
64
59
for (let i= 0 ; i< haystack .length ; i++ ) {
65
- let j= 0
66
-
67
- while (i+ j< haystack .length && haystack[i+ j]== needle[j]) {
68
- j+= 1
69
-
70
- if (j== needle .length ) {
71
- return i
72
- }
60
+ if (haystack .slice (i, i+ needle .length )== needle) {
61
+ return i
73
62
}
74
63
}
75
64
@@ -102,27 +91,7 @@ var strStr = function (haystack, needle) {
102
91
# Welcome to create a PR to complete the code of this language, thanks!
103
92
```
104
93
105
- ##C
106
- ``` c
107
- // Welcome to create a PR to complete the code of this language, thanks!
108
- ```
109
-
110
- ##Kotlin
111
- ``` kotlin
112
- // Welcome to create a PR to complete the code of this language, thanks!
113
- ```
114
-
115
- ##Swift
116
- ``` swift
117
- // Welcome to create a PR to complete the code of this language, thanks!
118
- ```
119
-
120
- ##Rust
121
- ``` rust
122
- // Welcome to create a PR to complete the code of this language, thanks!
123
- ```
124
-
125
- ##Other languages
94
+ ##C, Kotlin, Swift, Rust or other languages
126
95
```
127
96
// Welcome to create a PR to complete the code of this language, thanks!
128
97
```