1143. 最长公共子序列

链表算法题(程序员面试宝典)

解题思路主要来源于leetcode官方与《程序员面试宝典》。

1143. 最长公共子序列

给定两个字符串 text1 和 text2,返回这两个字符串的最长公共子序列的长度。

一个字符串的 子序列 是指这样一个新的字符串:它是由原字符串在不改变字符的相对顺序的情况下删除某些字符(也可以不删除任何字符)后组成的新字符串。
例如,“ace” 是 “abcde” 的子序列,但 “aec” 不是 “abcde” 的子序列。两个字符串的「公共子序列」是这两个字符串所共同拥有的子序列。

若这两个字符串没有公共子序列,则返回 0。

示例 1:

输入:text1 = “abcde”, text2 = “ace”
输出:3
解释:最长公共子序列是 “ace”,它的长度为 3。
示例 2:

输入:text1 = “abc”, text2 = “abc”
输出:3
解释:最长公共子序列是 “abc”,它的长度为 3。
示例 3:

输入:text1 = “abc”, text2 = “def”
输出:0
解释:两个字符串没有公共子序列,返回 0。

提示:

1 <= text1.length <= 1000
1 <= text2.length <= 1000
输入的字符串只含有小写英文字符。

解题方法

参考资料1:https://mp.weixin.qq.com/s?__biz=MzAxODQxMDM0Mw==&mid=2247484486&idx=1&sn=0bdcb94c6390307ea32427757ec0072c&chksm=9bd7fa4eaca073583623cdb93b05dc9e1d0757b25697bb40b29b3e450124e929ff1a8eaac50f&scene=21#wechat_redirect
参考资料2:https://mp.weixin.qq.com/s/ZhPEchewfc03xWv9VP3msg

解题思路1

递归解法

class Solution {
    public int longestCommonSubsequence(String text1, String text2) {
        //递归法
        return dp(text1,0,text2,0);
    }

    //dp函数表示:计算字符串text1[i,..] text2[j,..] 的最长公共子序列
    public int dp(String text1,int i,String text2,int j){
        //base 
        if(i==text1.length()||j==text2.length()){
            return 0;
        }

        if(text1.charAt(i)==text2.charAt(j)){
            //即出现在text1中也出现在text2中,则必然是最长公共序列中的数
            return 1 + dp(text1,i+1,text2,j+1);
        }else{
            //text1[i] 和 text2[j] 中至少有一个字符不在 lcs 中,
            //dp(text1,i+1,text2,j) 为 text1[i]不在 lcs 中
            //dp(text1,i,text2,j+1) 为 text2[j]不在 lcs 中
            return Math.max(dp(text1,i+1,text2,j),dp(text1,i,text2,j+1));
        }
    }
}

在这里插入图片描述

解题思路2

带备忘录的递归法

class Solution {
    int[][] memo;
    public int longestCommonSubsequence(String text1, String text2) {
        //备忘录
        memo = new int[text1.length()][text2.length()];
        //-1 表示未曾计算
        for(int[] row: memo){
            Arrays.fill(row,-1);
        }
        
        //带备忘录式递归法
        return dp(text1,0,text2,0);
    }

    //dp函数表示:计算字符串text1[i,..] text2[j,..] 的最长公共子序列
    public int dp(String text1,int i,String text2,int j){
        //base 
        if(i==text1.length()||j==text2.length()){
            return 0;
        }
        if(memo[i][j]!=-1){
            return memo[i][j];
        }
        if(text1.charAt(i)==text2.charAt(j)){
            //即出现在text1中也出现在text2中,则必然是最长公共序列中的数
            memo[i][j] = 1 + dp(text1,i+1,text2,j+1);
        }else{
            //text1[i] 和 text2[j] 中至少有一个字符不在 lcs 中,
            //情况1:dp(text1,i+1,text2,j) 为 text1[i]不在 lcs 中
            //情况2:dp(text1,i,text2,j+1) 为 text2[j]不在 lcs 中
            //情况3:dp(text1,i+1,text2,j+1) 为 text1[i],text2[j]不在 lcs 中 (不考虑)
            memo[i][j] = Math.max(dp(text1,i+1,text2,j),dp(text1,i,text2,j+1));
        }
        return memo[i][j];
    }
}

在这里插入图片描述

解题思路3

动态规划法

class Solution {
    public int longestCommonSubsequence(String text1, String text2) {
        //动态规划法
        //dp[i][j]  表示text1[1..i] 与 text2[1..j] 的最长公共子序列
        int[][] dp = new int[text1.length()+1][text2.length()+1];
        //base case dp[0][..]=0, dp[..][0]=0
        for(int[] row: dp){
            Arrays.fill(row,0);
        }

        for(int i=1;i<=text1.length();i++){
            for(int j=1;j<=text2.length();j++){
                if(text1.charAt(i-1)==text2.charAt(j-1)){
                    dp[i][j] = 1 + dp[i-1][j-1];
                }else{
                    dp[i][j] = Math.max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        return dp[text1.length()][text2.length()];
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值