原题链接在这里:https://leetcode.com/problems/longest-common-subsequence/

题目:

Given two strings text1 and text2, return the length of their longest common subsequence.

subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.

If there is no common subsequence, return 0.

Example 1:

Input: text1 = "abcde", text2 = "ace"
Output: 3
Explanation: The longest common subsequence is "ace" and its length is 3.

Example 2:

Input: text1 = "abc", text2 = "abc"
Output: 3
Explanation: The longest common subsequence is "abc" and its length is 3.

Example 3:

Input: text1 = "abc", text2 = "def"
Output: 0
Explanation: There is no such common subsequence, so the result is 0.

Constraints:

  • 1 <= text1.length <= 1000
  • 1 <= text2.length <= 1000
  • The input strings consist of lowercase English characters only.

题解:

dp[i][j] stands for length of LCS between text1 up to i and text2 up to j.

If text1.charAt(i) == text2.charAt(j), dp[i][j] = dp[i-1][j-1] + 1.

Otherwise, dp[i][j] = Math.max(dp[i][j-1], dp[i-1][j]).

如果不放心的话,就直接取上述三个的最小.

Time Complexity: O(m*n). m = text1.length. n = text2.length.

Space: O(m*n).

AC Java:

 class Solution {
public int longestCommonSubsequence(String text1, String text2) {
if(text1 == null || text1.length() == 0 || text2 == null || text2.length() == 0){
return 0;
} int m = text1.length();
int n = text2.length();
int [][] dp = new int[m+1][n+1];
for(int i = 0; i<m; i++){
for(int j = 0; j<n; j++){
dp[i+1][j+1] = Math.max(dp[i][j+1], dp[i+1][j]);
if(text1.charAt(i) == text2.charAt(j)){
dp[i+1][j+1] = Math.max(dp[i+1][j+1], dp[i][j]+1);
}
}
} return dp[m][n];
}
}

类似Longest PalindromicMaximum Length of Repeated Subarray.

LeetCode 1143. Longest Common Subsequence的更多相关文章

  1. 【leetcode】1143. Longest Common Subsequence

    题目如下: Given two strings text1 and text2, return the length of their longest common subsequence. A su ...

  2. 1143. Longest Common Subsequence

    link to problem Description: Given two strings text1 and text2, return the length of their longest c ...

  3. Leetcode: Longest Common Subsequence

    Given two strings text1 and text2, return the length of their longest common subsequence. A subseque ...

  4. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  5. LintCode Longest Common Subsequence

    原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...

  6. [UCSD白板题] Longest Common Subsequence of Three Sequences

    Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...

  7. LCS(Longest Common Subsequence 最长公共子序列)

    最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...

  8. Longest Common Subsequence

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

  9. Longest Common Subsequence & Substring & prefix

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

随机推荐

  1. Google Colab——零成本玩转深度学习

    前言 最近在学深度学习HyperLPR项目时,由于一直没有比较合适的设备训练深度学习的模型,所以在网上想找到提供模型训练,经过一段时间的搜索,最终发现了一个谷歌的产品--Google Colabora ...

  2. hadoop功能与用途

    1.hadoop是什么? 开源的分布式存储和分布式计算平台. 2.hadoop组成? HDFS:分布式文件存储系统,存储海量数据. Mapreduce:并行处理框架,实现任务分配和调度. 3.hado ...

  3. CLRS最大子数组问题

    今天我们一起来看一下关于最大子数组的一些问题.最大子数组的应用场景可以是这样的:有一天,你搞了一场投资开始炒股,这时你就会想,我怎样才能获得最大的利润呢,最简单的想法就是我在股票的最低价时买入,然后在 ...

  4. sql特殊日期

    --a. 本月的第一天 select dateadd(mm, datediff(mm,0,getdate()), 0) as 本月的第一天 --b. 本月的最后一天 select dateadd(ms ...

  5. 关于ElasticSearch的堆内存设置与优化

    1.什么是堆内存?Java 中的堆是 JVM 所管理的最大的一块内存空间,主要用于存放各种类的实例对象.在 Java 中,堆被划分成两个不同的区域:- 新生代 ( Young ).- 老年代 ( Ol ...

  6. C# vb .net实现黑白效果特效滤镜

    在.net中,如何简单快捷地实现Photoshop滤镜组中的黑白效果效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第 ...

  7. 学习笔记之大数据(Big Data)

    300 秒带你吃透大数据! https://mp.weixin.qq.com/s/VWaqRig6_JBNYC1NX7NQ-Q 手把手教你入门Hadoop(附代码&资源) https://mp ...

  8. linux技能四 用户管理

    用户管理:用户类型,添加用户,修改用户,删除用户,查看用户信息,用户的切换,添加组,修改组,删除组,查看组 用户类型:超级用户:root,UID=1 系统用户:运行系统服务的,不能登陆的,UID=(1 ...

  9. nodejs SSL Error: CERT_UNTRUSTED while using npm command 错误

    SSH 使用错误,其实我们关掉HTTPS就好了 npm config set strict-ssl false 或者 npm config set registry="http://regi ...

  10. js 数组去重求和 (转载)

    方法一:js数组id去重,value值相加问题 来源:https://www.jianshu.com/p/8f79e31b46ed // js let arr = [ { id: 1, value: ...