原题链接在这里: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. Centos7下JDK1.8的安装

    1.下载并上传并解压安装包 下载安装包上传到/usr/local目录 https://www.oracle.com/technetwork/java/javase/downloads/jdk8-dow ...

  2. 关于一致性hash,这可能是全网最形象生动最容易理解的文档,想做架构师的你来了解一下

    问题提出 一致性hash是什么?假设有4台缓存服务器N0,N1,N2,N3,现在需要存储数据OBJECT1,OBJECT2,OBJECT3,OBJECT4,OBJECT5,OBJECT5,OBJECT ...

  3. VMware Workstation 15 Player使用centos页面版本如何查看ip

    首先运行要使用的centos镜像,输入密码登陆进去 因为是界面版,所以就不需要再镜像中输入命令,但是因为这样又找不到没法用ifconfig查看ip怎么办?  这个就是类似于一个系统页面版本的linux ...

  4. 个人Wiki搭建(Gitbook + GitHub Pages)

    工具选择:Gitbook + GitHub Pages 大概流程: 首先在本地编写md文件,然后生成对应的html文件,最后将这些html文件推送到github对应的gitbook仓库. 具体步骤: ...

  5. 利用jQuery-Word-Export导出word (含ECharts)

      写在前面的话:写博客的初衷是想把自己学到的知识总结下来,在写的过程中,相当于又把知识梳理了一遍.我坚信有输入,有输出,技术才会进步.我一般都会自己写一个小demo,测试没有问题,再进行整理. 在实 ...

  6. nginx.conf配置demo

    #user nobody; worker_processes 4; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...

  7. python写文件无法换行的问题

    python写文件无法换行的问题,用'\n'  不行,直接打印的出来了. 网上查了查,都说是用  ‘\r\n’ ,但是这样打出来,不仅换行了,还加了一个空行. windows平台最后结果是    直接 ...

  8. Python进阶(十)----软件开发规范, time模块, datatime模块,random模块,collection模块(python额外数据类型)

    Python进阶(十)----软件开发规范, time模块, datatime模块,random模块,collection模块(python额外数据类型) 一丶软件开发规范 六个目录: #### 对某 ...

  9. WinRAR捆绑木马

    准备好木马文件 server.exe 准备一个小游戏 趣味数学计算 压缩 创建自解压格式压缩文件 自解压选项设置 解压路径设置 设置程序 模式设置 压缩完成 使用 开始玩游戏

  10. 机智云连接esp8266--远程控制风扇转速

    概述 下面我们使用esp8266开发板和机智云云端,实现如何将一个USB风扇,改造成可以远程控制转速的智能风扇. 1.准备工作 硬件: (1)esp8266开发板 (2)USB线 (3)USB风扇 软 ...