Python 56 ms Time O(N*M) Space O(1)

  • 12

    Do we need to really use KMP in the interview? I just had a few interviews but personally I really can not remember those fantastic algorithms in that short period of time in pressure. Maybe I was nervous and needed more programming practice..

    def strStr(self, haystack, needle):
        if needle == "":
            return 0
        for i in range(len(haystack)-len(needle)+1):
            for j in range(len(needle)):
                if haystack[i+j] != needle[j]:
                if 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.