题解 | 最长公共子串

最长公共子串

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

l作为左指针,i作为右指针,不断更新窗口,如果1窗口中的字符串出现在2中,则右指针向右移,不然则左指针向右移,

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# longest common substring
# @param str1 string字符串 the string
# @param str2 string字符串 the string
# @return string字符串
#
class Solution:
    def LCS(self , str1: str, str2: str) -> str:
        # write code here
        ans, l, n = '', 0, len(str1)
        for i in range(n+1):
            if str1[l:i] in str2:
                ans = str1[l:i]
            else:
                l += 1
        return ans

全部评论

相关推荐

01-12 09:24
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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