Longest Common Substring.

Θ(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.)