My Go solution.


  • 0
    Y
    func strStr(haystack string, needle string) int {
    	if needle == "" {
    		return 0
    	}
    
    	for index, letter := range(haystack) {
    		if index <= len(haystack) - len(needle) && letter == rune(needle[0]) {
    			if haystack[index:index+len(needle)] == needle {
    				return index
    			}
    		}
    	}
    	return -1
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.