LintCode_chapter1_section5_longest-common-substring

coding = utf-8

    '''
    Created on 2015年11月6日
    
    @author: SphinxW
    '''
    # 長公共子串
    #
    # 給出兩個字符串,找到最長公共子串,并返回其長度。
    #
    #
    # 您在真實的面試中是否遇到過這個題?
    # 樣例
    #
    # 給出A=“ABCD”,B=“CBCE”,返回 2
    # 注意
    #
    # 子串的字符應(yīng)該連續(xù)的出現(xiàn)在原字符串中,這與子序列有所不同。
    
    
    class Solution:
        # @param A, B: Two string.
        # @return: the length of the longest common substring.
    
        def longestCommonSubstring(self, A, B):
            if len(B) == 0:
                return 0
            # write your code here
            for length in range(1, len(B) + 1)[::-1]:
                for index in range(len(B) - length + 1):
                    if A.find(B[index:index + length]) == -1:
                        pass
                    else:
                        return length
            return 0
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容