Created
August 20, 2024 12:20
-
-
Save mazdak78/0bee3be88cfc812ce7681103d214b388 to your computer and use it in GitHub Desktop.
leetcode - Find the index of first occurrence in a string - golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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