Python 题解 Code View | #最长公共子串#

最长公共子串

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

class Solution:
    def LCS(self , str1 , str2 ):
        # write code here
        l1, l2 = len(str1), len(str2)
        dp = [['' for _ in range(l2 + 1)] for _ in range(l1 + 1)]
        ans = ''
        for i in range(l1):
            for j in range(l2):
                if str1[i] == str2[j]:
                    dp[i+1][j+1] = dp[i][j] + str1[i]
                    if len(dp[i+1][j+1]) > len(ans):
                        ans = dp[i+1][j+1]
        return ans
全部评论
超时了
点赞 回复 分享
发布于 2022-04-03 20:50

相关推荐

评论
5
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务