Longest Common Substring (Shortest Common Superstring)

https://leetcode.com/problems/shortest-common-supersequence/

这两个问题是相同的其实

以下是Bottom-up DP的做法

class Solution:
    def shortestCommonSupersequence(self, str1: str, str2: str) -> str:
        def lcs(str1,str2):
            m = len(str1)
            n = len(str2)
            dp= [[""]*(1+n) for _ in range(m+1)]
            for i in range(1,m+1):
                for j in range(1,n+1):
                    if str1[i-1] == str2[j-1]:
                        dp[i][j] = dp[i-1][j-1] + str1[i-1]
                    else:
                        dp[i][j] = max(dp[i-1][j],dp[i][j-1],key=len)
            return dp[-1][-1]
        
        res =""
        i = 0
        j = 0
        #print(lcs(str1,str2))
        for c in lcs(str1,str2):
            while str1[i] != c:
                res+=str1[i]
                i+=1
            while str2[j] != c:
                res+= str2[j]
                j+=1
            res += c
            i+=1
            j+=1
        
        return res + str1[i:] + str2[j:]

当然用recursion也是可以做的,需要用一个cache来存之前recursion计算过的值这样可以把recursion的O(2^N)的降为O(MN)

这个recursion若用DP做法如下

Last updated