Share my 44ms python codde

  • 0
    def strStr(self, haystack, needle):
        l1 = len(needle)
        l2 = len(haystack)
        for i in xrange(l2 - l1 + 1):
            if needle == haystack[i:i + l1]:
                return i
        return -1

    there's also a build-in method find(str)
    just one line, nearly 55ms time costs.

    return haystack.find(needle)

Log in to reply

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