leetcood学习笔记-28-KMP*

题目:

第一次提交:

class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        if not len(needle):
            return 0
        for i in range(len(haystack)):
            if i+len(needle)<=len(haystack):
                if haystack[i:(i+len(needle))]==needle:
                    return i
        return -1

方法二: Sunday 平均O(N),最坏O(MN)

class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        if not needle:return 0
        from collections import defaultdict
        dic = defaultdict(int)
        for i,v in enumerate(needle):
            dic[v] = len(needle) - i
        idx = 0
        while idx + len(needle) <= len(haystack):
            cur = haystack[idx:idx+len(needle)]
            if cur == needle:
                return idx
            else:
                if idx+len(needle) >= len(haystack):
                    return -1
                cur_c = haystack[idx+len(needle)]
                if dic.get(cur_c):
                    idx += dic[cur_c]
                else:
                    idx += len(needle)+1
        return -1

方法三:kmp O(m + n)

class Solution:
    def strStr(self, t, p):
        """
        :type haystack: str
        :type needle: str
        :rtype: int
        """
        if not p : return 0
        _next = [0] * len(p)

        def getNext(p, _next):
            _next[0] = -1
            i = 0
            j = -1
            while i < len(p) - 1:
                if j == -1 or p[i] == p[j]:
                    i += 1
                    j += 1
                    _next[i] = j
                else:
                    j = _next[j]
        getNext(p, _next)
        i = 0
        j = 0
        while i < len(t) and j < len(p):
            if j == -1 or t[i] == p[j]:
                i += 1
                j += 1
            else:
                j = _next[j]
        if j == len(p):
            return i - j
        return -1

next数组的优化:

class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        if not needle:return 0
        def getNext(s):
            next_ = [0] * len(s)
            next_[0] = -1
            i = 0
            j = -1
            while i < len(s) - 1:
                if j == -1 or s[i] == s[j]:
                    j += 1
                    i += 1
                    if s[i] == s[j]:
                        next_[i] = next_[j]
                    else:
                        next_[i] = j
                else:
                    j = next_[j]
            return next_
        next_ = getNext(needle)
        i,j = 0,0
        while i < len(haystack) and j < len(needle):
            if j == -1 or haystack[i] == needle[j]:
                i += 1
                j += 1
            else:
                j = next_[j]
        if j == len(needle):
            return i - j
        return -1
            

 

注意:

说明:

当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。

对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。

方法二:KMP算法,KMP的整体时间复杂度为O(m + n)。

算法详解:https://blog.csdn.net/v_july_v/article/details/7041827

posted @ 2019-03-11 13:37  oldby  阅读(208)  评论(0编辑  收藏  举报