Skip to content

Commit 529b253

Browse files
committed
go: add 0028 Find The Index of The First Occurence in a String
1 parent 7d1802d commit 529b253

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)