← All solutionsFind 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.
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
}
Performance
- Runtime beats: 100%
- Memory beats: 50%
Complexity
LeetCode Problem Link