Skip to content

Commit 554b82e

Browse files
authored
Merge pull request neetcode-gh#1838 from josuebrunel/feat/0028-Find-The-Index-of-The-First-Occurence-in-a-String.go
Feat/0028 find the index of the first occurence in a string.go
2 parents c6b7428 + 529b253 commit 554b82e

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func strStr(haystack string, needle string) int {
2+
if haystack == "" && needle == "" {
3+
return 0
4+
}
5+
lh := len(haystack)
6+
ln := len(needle)
7+
for i := 0; i < lh; i++ {
8+
ct := 0
9+
for j := 0; j < ln; j++ {
10+
if i+j >= lh {
11+
return -1
12+
}
13+
if needle[j] != haystack[i+j] {
14+
break
15+
}
16+
ct++
17+
}
18+
if ct == ln {
19+
return i
20+
}
21+
}
22+
return -1
23+
}

0 commit comments

Comments
 (0)