Problem List
Find The Index Of The First Occurence In A String
January 30, 2025 • Go • strings • easy
Problem
- 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.
Performance
- Runtime beats: 100%
- Memory beats: 50%
Complexity
Go Solution
func strStr(haystack string, needle string) int {
i:= 0
for len(needle) <= len(haystack) - i {
if needle == haystack[i: i+ len(needle)] {
return i
}
i++
}
return -1
}
LeetCode Problem Link