原题链接在这里: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. unity---为什么用Time.deltaTime * speed 表示每秒移动的距离的理解

    Time.deltaTime:代表时间增量,即从上一帧到当前帧消耗的时间, 这个值是动态变化的. dt 表示 deltaTime. 假如 1s渲染10帧,沿X轴方向的移动速度 speed = 10m/ ...

  2. 搭建wildfly domain集群

    两台机器为参考,wildfly版本为wildfly-9.0.1.Final,10版本用本方法有点问题. master:192.168.0.1 slave :192.168.0.2 master: cd ...

  3. Oracle数据库Schema的简介

    百度文库中 Schema 的解释: 数据库中的Schema,为数据库对象的集合,一个用户一般对应一个schema. 官方定义如下: A schema is a collection of databa ...

  4. jupyter notebook在 mac 使用

    1. 查看当前 conda 所拥有的环境列表 conda env list 2. 选择要进入的环境 source activate your_env_name 3. 启动 jupyter jupyte ...

  5. Java abstract 理解和学习

    /** * <html> * <body> * <P> Copyright JasonInternational Since 1994 https://github ...

  6. 换个语言学一下 Golang (7)——使用函数

    什么是函数 函数,简单来讲就是一段将输入数据转换为输出数据的公用代码块.当然有的时候函数的返回值为空,那么就是说输出数据为空.而真正的处理过程在函数内部已经完成了. 想一想我们为什么需要函数,最直接的 ...

  7. js正则表达式【续】(相关字符的解释含义)

    1.字符类[直接量] . (点号,小数点) 匹配任意单个字符,但是行结束符除外\d 匹配一个0-9之间的阿拉伯数字.等价于[0-9]\D    匹配任意一个不是0-9之间阿拉伯数字的字符.等价于[^0 ...

  8. 关于 Safari 浏览器不支持 location [ window.location.href window.open()] 跳转问题的解决方案

    最近在做项目时,碰到 safari 浏览器不支持location跳转问题,针对此问题,可以通过 js 模拟点击时间来解决,代码如下: <!DOCTYPE HTML> <html la ...

  9. 彻底弄懂ES6中的Map和Set

    Map Map对象保存键值对.任何值(对象或者原始值) 都可以作为一个键或一个值.构造函数Map可以接受一个数组作为参数. Map和Object的区别 一个Object 的键只能是字符串或者 Symb ...

  10. Html-元素类型笔记

    注意点: 元素类型分为 块级元素 和 行内元素 块级元素: 在网页中以块的形式显示,默认情况都会占据一行,两个相邻的块级元素不会出现并列显示的元素,按照顺序自上而下排列. 块级元素可以定义自己的宽度和 ...