最长公共子序列、交叉字符串

77. 最长公共子序列

给出两个字符串,找到最长公共子序列(LCS),返回LCS的长度。

样例
样例 1:
输入: “ABCD” and “EDCA”
输出: 1

解释:
LCS 是 'A' 或  'D' 或 'C'

样例 2:
输入: “ABCD” and “EACB”
输出: 2

解释: 
LCS 是 "AC"

说明
最长公共子序列的定义:

最长公共子序列问题是在一组序列(通常2个)中找到最长公共子序列(注意:不同于子串,LCS不需要是连续的子串)。该问题是典型的计算机科学问题,是文件差异比较程序的基础,在生物信息学中也有所应用。
https://en.wikipedia.org/wiki/Longest_common_subsequence_problem

class Solution:
    """
    @param A: A string
    @param B: A string
    @return: The length of longest common subsequence of A and B
    a[i][j]代表前A[i]个字符和前B[j]个字符的最长公共子序列
    对于第i个字符和第j个字符而言,
    其相等,则a[i][j] = a[i-1][j-1] + 1
    其不相等,则a[i][j] = max(a[i-1][j], a[i][j-1])
    """
    def longestCommonSubsequence(self, A, B):
        # write your code here
        n, m = len(A), len(B)
        
        a = [[0 for _ in range(n+1)] for _ in range(m+1)]
        
        for i in range(1, n+1):
            for j in range(1, m+1):
                if A[i-1] == B[j-1]:
                    a[i][j] = a[i-1][j-1] + 1
                else:
                    a[i][j] = max(a[i-1][j], a[i][j-1])
        
        return a[n][m]
        

29. 交叉字符串

中文English
给出三个字符串:s1、s2、s3,判断s3是否由s1和s2交叉构成。

样例
样例 1:

输入:
“aabcc”
“dbbca”
“aadbbcbcac”
输出:
true
样例 2:

输入:
“”
“”
“1”
输出:
false
样例 3:

输入:
“aabcc”
“dbbca”
“aadbbbaccc”
输出:
false
挑战
要求时间复杂度为O(n2)或者更好

分析

 a[s][i][j]表示X的前s个字符是否由A的前i个字符和B的前j个字符组成
    其中s=i+j,故省去s
    a[i][j]表示X的前i+j个字符是否由A的前i个字符和B的前j个字符组成
    当X[i+j] == A[i] and a[i-1][j] or X[i+j]==B[j] and a[i][j-1]时 a[i][j]=True
    注意边界条件:
    A,B两个都为空,为true
    A,B只有一个为空,为true
#原始解法
class Solution:
    """
    @param s1: A string
    @param s2: A string
    @param s3: A string
    @return: Determine whether s3 is formed by interleaving of s1 and s2
    """
    def isInterleave(self, s1, s2, s3):
        # write your code here
        n, m = len(s1), len(s2)
    	
        if m+n == 0 or m+n == 1:
            return True
            
        if n + m != len(s3):
            return False
        
        s1 = '0'+s1
        s2 = '0'+s2
        s3 = '0' + s3
        a = [[False for _ in range(m+1)] for _ in range(n+1)]
        
        a[0][0] = True   
        
        for i in range(1, n+1):
            for j in range(0, m+1):
                t1, t2= False, False
                if s3[i+j] == s1[i]:
                    t1 = a[i-1][j]
                if s3[i+j] == s2[j]:
                    t2 = a[i][j-1]
                    
                a[i][j] = t1 or t2

        
        return a[n][m]
#按照递推方程
class Solution:
    """
    @param s1: A string
    @param s2: A string
    @param s3: A string
    @return: Determine whether s3 is formed by interleaving of s1 and s2
   
    """
    def isInterleave(self, s1, s2, s3):
        # write your code here
        n, m = len(s1), len(s2)
        #or m+n == 1
        if m+n == 0 :
            return True
            
        if n + m != len(s3):
            return False
        
        s1 = '0'+s1 #加0方便后续处理(字符串从位置1开始)
        s2 = '0'+s2
        s3 = '0' + s3
        a = [[False for _ in range(m+1)] for _ in range(n+1)]
        
        a[0][0] = True
        #考虑当s1和s2之中有一个为空字符时,也是能够组成交叉字符串的
        for i in range(1,n+1):
            #当s2=""时
            if s3[i] == s1[i] and a[i-1][0]:
                a[i][0] = True
        
        for j in range(1, m+1):
            #当s1=""时,
            if s3[j] == s2[j] and a[0][j-1]:
                a[0][j] = True
        
        for i in range(1, n+1):
            for j in range(0, m+1):
                
                if (s3[i+j] == s1[i] and a[i-1][j] ) or (s3[i+j] == s2[j] and a[i][j-1]):
                    a[i][j] = True
        
        return a[n][m]

附讲解
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值