Brute force Python solution

  • 0
    def strStr(self, haystack, needle):
            if not needle: return 0
            for i in range(len(haystack) - len(needle) + 1):
                for j in range(len(needle)):
                    if haystack[i + j] != needle[j]:
                    elif j == len(needle) - 1:
                        return i
            return -1

Log in to reply

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