You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Original link:[leetcoder.net - Fucking Good LeetCode Solutions](https://leetcoder.net/en/leetcode/28-find-the-index-of-the-first-occurrence-in-a-string)
1
+
Original link:[leetcoder.net -LeetCoder:Fucking Good LeetCode Solutions](https://leetcoder.net/en/leetcode/28-find-the-index-of-the-first-occurrence-in-a-string)
2
2
3
-
#28. Find the Index of the First Occurrence in a String - Fucking Good LeetCode Solutions
4
-
LeetCode link:[28. Find the Index of the First Occurrence in a String](https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string),difficulty:**Easy**.
3
+
#28. Find the Index of the First Occurrence in a String -LeetCoder:Fucking Good LeetCode Solutions
4
+
LeetCode link:[28. Find the Index of the First Occurrence in a String](https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string),Difficulty:**Easy**.
5
5
6
6
##LeetCode description of "28. Find the Index of the First Occurrence in a String"
7
7
Given two strings`needle` and`haystack`, return the**index** of the first occurrence of`needle` in`haystack`, or`-1` if`needle` is not part of`haystack`.
@@ -29,7 +29,6 @@ The first occurrence is at index 0, so we return 0.
29
29
-`haystack` and`needle` consist of only lowercase English characters.
30
30
31
31
##Intuition
32
-
33
32
- This kind of question can be solved with one line of code using the built-in`index()`. Obviously, the questioner wants to test our ability to control the loop.
34
33
35
34
- For`heystack`, traverse each character in turn. There may be two situations:
@@ -39,59 +38,39 @@ The first occurrence is at index 0, so we return 0.
39
38
- This question is easier to understand by looking at the code directly.
40
39
41
40
##Complexity
42
-
* Time:`O(m +n)`.
43
-
* Space:`O(n)`.
41
+
- Time complexity:`O(N +M)`.
42
+
- Space complexity:`O(1)`.
44
43
45
44
##Python
46
45
```python
47
46
classSolution:
48
47
defstrStr(self,haystack:str,needle:str) ->int:
49
48
for iinrange(len(haystack)):
50
-
if haystack[i:i+len(needle)]== needle:
51
-
return i
52
-
49
+
j=0
50
+
while i+ j<len(haystack)and haystack[i+ j]== needle[j]:
51
+
j+=1
52
+
if j==len(needle):
53
+
return i
53
54
return-1
54
55
```
55
56
56
57
##JavaScript
57
58
```javascript
58
59
varstrStr=function (haystack,needle) {
59
60
for (let i=0; i<haystack.length; i++) {
60
-
if (haystack.slice(i, i+needle.length)== needle) {
61
-
return i
61
+
let j=0
62
+
while (i+ j<haystack.length&& haystack[i+ j]== needle[j]) {
63
+
j+=1
64
+
if (j==needle.length) {
65
+
return i
66
+
}
62
67
}
63
68
}
64
-
65
69
return-1
66
70
};
67
71
```
68
72
69
-
##C++
70
-
```cpp
71
-
// Welcome to create a PR to complete the code of this language, thanks!
72
-
```
73
-
74
-
##Java
73
+
##Other languages
75
74
```java
76
75
// Welcome to create a PR to complete the code of this language, thanks!
77
76
```
78
-
79
-
##C#
80
-
```c#
81
-
// Welcome to create a PR to complete the code of this language, thanks!
82
-
```
83
-
84
-
##Go
85
-
```go
86
-
// Welcome to create a PR to complete the code of this language, thanks!
87
-
```
88
-
89
-
##Ruby
90
-
```ruby
91
-
# Welcome to create a PR to complete the code of this language, thanks!
92
-
```
93
-
94
-
##C, Kotlin, Swift, Rust or other languages
95
-
```
96
-
// Welcome to create a PR to complete the code of this language, thanks!