Skip to content

Commit 1ad397d

Browse files
committed
feat: add ts solution to lc problem: No.0028
No.0028.Implement strStr()
1 parent a407222 commit 1ad397d

File tree

3 files changed

+424
-1
lines changed

3 files changed

+424
-1
lines changed

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

Lines changed: 204 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,218 @@
6161
<!-- 这里可写当前语言的特殊实现逻辑 -->
6262

6363
```python
64-
64+
class Solution:
65+
def strStr(self, haystack, needle):
66+
"""
67+
:type haystack: str
68+
:type needle: str
69+
:rtype: int
70+
"""
71+
for i in range(len(haystack) - len(needle) + 1):
72+
p = i
73+
q = 0
74+
while p < len(haystack) and q < len(needle) and haystack[p] == needle[q]:
75+
p += 1
76+
q += 1
77+
78+
if q == len(needle):
79+
return i
80+
81+
return -1
6582
```
6683

6784
### **Java**
6885

6986
<!-- 这里可写当前语言的特殊实现逻辑 -->
7087

7188
```java
89+
class Solution {
90+
public int strStr(String haystack, String needle) {
91+
if ("".equals(needle)) {
92+
return 0;
93+
}
94+
95+
int len1 = haystack.length();
96+
int len2 = needle.length();
97+
int p = 0;
98+
int q = 0;
99+
while (p < len1) {
100+
if (haystack.charAt(p) == needle.charAt(q)) {
101+
if (len2 == 1) {
102+
return p;
103+
}
104+
++p;
105+
++q;
106+
} else {
107+
p -= q - 1;
108+
q = 0;
109+
}
110+
111+
if (q == len2) {
112+
return p - q;
113+
}
114+
}
115+
return -1;
116+
}
117+
}
118+
```
119+
120+
### **C++**
121+
122+
```cpp
123+
class Solution {
124+
private:
125+
vector<int> Next(string str)
126+
{
127+
vector<int> n(str.length()) ;
128+
n[0] = -1 ;
129+
int i = 0, pre = -1 ;
130+
int len = str.length() ;
131+
while (i < len)
132+
{
133+
while (pre >= 0 && str[i] != str[pre])
134+
pre = n[pre] ;
135+
++i, ++pre ;
136+
if (i >= len)
137+
break ;
138+
if (str[i] == str[pre])
139+
n[i] = n[pre] ;
140+
else
141+
n[i] = pre ;
142+
}
143+
return n ;
144+
}
145+
public:
146+
int strStr(string haystack, string needle) {
147+
if (0 == needle.length())
148+
return 0 ;
149+
150+
vector<int> n(Next(needle)) ;
151+
152+
int len = haystack.length() - needle.length() + 1 ;
153+
for (int i = 0; i < len; ++i)
154+
{
155+
int j = 0, k = i ;
156+
while (j < needle.length() && k < haystack.length())
157+
{
158+
if (haystack[k] != needle[j])
159+
{
160+
if (n[j] >= 0)
161+
{
162+
j = n[j] ;
163+
continue ;
164+
}
165+
else
166+
break ;
167+
}
168+
++k, ++j ;
169+
}
170+
if (j >= needle.length())
171+
return k-j ;
172+
}
173+
174+
return -1 ;
175+
}
176+
};
177+
```
178+
179+
### **C#**
180+
181+
```cs
182+
public class Solution {
183+
public int StrStr(string haystack, string needle) {
184+
for (var i = 0; i < haystack.Length - needle.Length + 1; ++i)
185+
{
186+
var j = 0;
187+
for (; j < needle.Length; ++j)
188+
{
189+
if (haystack[i + j] != needle[j]) break;
190+
}
191+
if (j == needle.Length) return i;
192+
}
193+
return -1;
194+
}
195+
}
196+
```
197+
198+
### **Go**
199+
200+
```go
201+
func strStr(haystack string, needle string) int {
202+
switch {
203+
case len(needle) == 0:
204+
return 0
205+
case len(needle) > len(haystack):
206+
return -1
207+
case len(needle) == len(haystack):
208+
if needle == haystack {
209+
return 0
210+
}
211+
return -1
212+
}
213+
cursor := 0
214+
for i := 0; i < len(haystack); i++ {
215+
if haystack[i] == needle[cursor] {
216+
cursor++
217+
if cursor == len(needle) {
218+
return i - cursor + 1
219+
}
220+
} else {
221+
i -= cursor
222+
cursor = 0
223+
}
224+
}
225+
return -1
226+
}
227+
```
228+
229+
### **JavaScript**
230+
231+
```js
232+
/**
233+
* @param {string} haystack
234+
* @param {string} needle
235+
* @return {number}
236+
*/
237+
var strStr = function (haystack, needle) {
238+
const slen = haystack.length;
239+
const plen = needle.length;
240+
if (slen == plen) {
241+
return haystack == needle ? 0 : -1;
242+
}
243+
for (let i = 0; i <= slen - plen; i++) {
244+
let j;
245+
for (j = 0; j < plen; j++) {
246+
if (haystack[i + j] != needle[j]) {
247+
break;
248+
}
249+
}
250+
if (j == plen) return i;
251+
}
252+
return -1;
253+
};
254+
```
72255

256+
### **TypeScript**
257+
258+
```ts
259+
function strStr(haystack: string, needle: string): number {
260+
const m = haystack.length;
261+
const n = needle.length;
262+
for (let i = 0; i <= m - n; i++) {
263+
let isEqual = true;
264+
for (let j = 0; j < n; j++) {
265+
if (haystack[i + j] !== needle[j]) {
266+
isEqual = false;
267+
break;
268+
}
269+
}
270+
if (isEqual) {
271+
return i;
272+
}
273+
}
274+
return -1;
275+
}
73276
```
74277

75278
### **...**

0 commit comments

Comments
 (0)