My Golang solution


  • 0
    R
    func strStr(haystack string, needle string) int {
    	if len(haystack) < len(needle) {
    		return -1
    	}
    	if len(needle) == 0{
    		return 0
    	}
    
    	for i := 0; i <= len(haystack)-len(needle); i++ {
    		if haystack[i:i+len(needle)] == needle {
    			return i
    		}
    	}
    
    	return -1
    }
    

Log in to reply
 

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