题解 | #最长公共子串#

最长公共子串

https://www.nowcoder.com/practice/f33f5adc55f444baa0e0ca87ad8a6aac

class Solution:
    def LCS(self , str1: str, str2: str) -> str:
        if len(str1) < len(str2): # 从长度大的字符串开始遍历
            str1, str2 = str2, str1

        max_len = 0 # 记录最大子串的长度
        res = ''

        for i in range(len(str1)): # 遍历一个字符串,遍历到的字符当作子串的最后一个字符,往前推,如果后面有比前面更长的子串就记录下来
            if str1[i-max_len:i+1] in str2:
                res = str1[i-max_len:i+1]
                max_len += 1 
                
        return res

全部评论

相关推荐

兄弟们,实习都是在接各种api,该怎么包装简历
仁者伍敌:感觉我自己做小项目也是各种api啊,我要怎么包装简历
点赞 评论 收藏
分享
07-01 23:23
郑州大学 Java
否极泰来来来来:牛客迟早有高三的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务