79. 最长公共子串

79. 最长公共子串

中文English

给出两个字符串,找到最长公共子串,并返回其长度。

样例

样例 1:
	输入:  "ABCD" and "CBCE"
	输出:  2
	
	解释:
	最长公共子串是 "BC"


样例 2:
	输入: "ABCD" and "EACB"
	输出:  1
	
	解释: 
	最长公共子串是 'A' 或 'C' 或 'B'

挑战

O(n x m) time and memory.

注意事项

子串的字符应该连续的出现在原字符串中,这与子序列有所不同。

输入测试数据 (每行一个参数)如何理解测试数据?
#最长公共子串
class Solution:
    """
    大致思路:
    1. 依次切割B出来,切割成各个子串,然后在A中进行判断,如果符合,则直接返回
    """
    def longestCommonSubstring(self, A, B):
        # write your code here
        if not A or not B:return 0

        l = len(A) if len(A) < len(B) else len(B)
        for index in range(l-1,-1,-1):
            print(index,len(B) - index)
            for i in range(len(B)-index):
                cut_s = B[i:i + index + 1]
                print(cut_s)
                if cut_s in A:
                    return len(cut_s)
原文地址:https://www.cnblogs.com/yunxintryyoubest/p/12995778.html