美文网首页
Longest Common Substring.

Longest Common Substring.

作者: R0b1n_L33 | 来源:发表于2018-03-19 23:52 被阅读9次

    Θ(m*n) where m, n are the length of two respect string.

    def longestCommonSubstringOf(str1:str, str2:str) -> int:
        # str1-length number of rows & str2-length number of columns
        matrix = [[0]*len(str2) for _ in range(len(str1))]
        maxWeight = 0
        rump = 0
        for i in range(len(str1)):
            for j in range(len(str2)):
                if str1[i] == str2[j]:
                    if i == 0 or j == 0:
                        matrix[i][j] = 1
                    else:
                        matrix[i][j] = matrix[i-1][j-1] + 1
                    if matrix[i][j] > maxWeight:
                        maxWeight = matrix[i][j]
                        rump = i
        return maxWeight
    

    相关文章

      网友评论

          本文标题:Longest Common Substring.

          本文链接:https://www.haomeiwen.com/subject/huwmqftx.html