Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mazdak78/0bee3be88cfc812ce7681103d214b388 to your computer and use it in GitHub Desktop.
Save mazdak78/0bee3be88cfc812ce7681103d214b388 to your computer and use it in GitHub Desktop.
leetcode - Find the index of first occurrence in a string - golang
func strStr(haystack string, needle string) int {
if len(haystack) < len(needle) {
return -1
}
needleBytes := needle[0:]
for i := 0; i < len(haystack); i++ {
if haystack[i:i+len(needle)] == needleBytes {
return i
}
if len(haystack[i:]) <= len(needleBytes) {
break
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment