Implement strStr()

ID: 28; easy

Solution 1

func strStr(haystack string, needle string) int {
    ln, lh := len(needle), len(haystack)
    if ln == 0 {
        return 0
    }
    if ln > lh {
        return -1
    }
    for i,_ := range haystack {
        if haystack[i] == needle[0] && i+ln <= lh && haystack[i+ln-1] == needle[ln-1] {
            if haystack[i:i+ln] == needle {
                return i
            }
        }
    }
    return -1
}

Solution 2

Solution 3

Not really a "solution"...

Last updated

Was this helpful?