Leetcode: Longest Common Subsequence
Given two strings text1 and text2, return the length of their longest common subsequence. A 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.
DP with a 2D array
Time & space: O(m * n)
class Solution {
public int longestCommonSubsequence(String text1, String text2) {
if (text1 == null || text1.length() == 0 || text2 == null || text2.length() == 0)
return 0;
int[][] dp = new int[text1.length() + 1][text2.length() + 1];
for (int i = 1; i <= text1.length(); i ++) {
for (int j = 1; j <= text2.length(); j ++) {
int val = Math.max(dp[i - 1][j], dp[i][j - 1]);
if (text1.charAt(i - 1) == text2.charAt(j - 1)) {
val = Math.max(val, dp[i - 1][j - 1] + 1);
}
dp[i][j] = val;
}
}
return dp[text1.length()][text2.length()];
}
}
(Skim through) memory optimization, referencing: https://leetcode.com/problems/longest-common-subsequence/discuss/351689/Java-Two-DP-codes-of-O(mn)-and-O(min(m-n))-spaces-w-picture-and-analysis
Obviously, the code in method 1 only needs information of previous row to update current row. So we just use a two-row 2D array to save and update the matching results for chars in s1 and s2.
Note: use k ^ 1 and k ^= 1 to switch between dp[0] (row 0) and dp[1] (row 1).
public int longestCommonSubsequence(String s1, String s2) {
int m = s1.length(), n = s2.length();
if (m < n) return longestCommonSubsequence(s2, s1);
int[][] dp = new int[2][n + 1];
for (int i = 0, k = 1; i < m; ++i, k ^= 1)
for (int j = 0; j < n; ++j)
if (s1.charAt(i) == s2.charAt(j)) dp[k][j + 1] = 1 + dp[k ^ 1][j];
else dp[k][j + 1] = Math.max(dp[k ^ 1][j + 1], dp[k][j]);
return dp[m % 2][n];
}
Leetcode: Longest Common Subsequence的更多相关文章
- LeetCode 1143. Longest Common Subsequence
原题链接在这里:https://leetcode.com/problems/longest-common-subsequence/ 题目: Given two strings text1 and te ...
- 【leetcode】1143. Longest Common Subsequence
题目如下: Given two strings text1 and text2, return the length of their longest common subsequence. A su ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- LintCode Longest Common Subsequence
原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...
- [UCSD白板题] Longest Common Subsequence of Three Sequences
Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...
- LCS(Longest Common Subsequence 最长公共子序列)
最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...
- Longest Common Subsequence
Given two strings, find the longest common subsequence (LCS). Your code should return the length of ...
- Longest Common Subsequence & Substring & prefix
Given two strings, find the longest common subsequence (LCS). Your code should return the length of ...
- Dynamic Programming | Set 4 (Longest Common Subsequence)
首先来看什么是最长公共子序列:给定两个序列,找到两个序列中均存在的最长公共子序列的长度.子序列需要以相关的顺序呈现,但不必连续.例如,"abc", "abg", ...
随机推荐
- Maven01-maven打包Web项目成war文件-tomcat脱机运行启动项目
1 执行package 2 复制 3 catalina run ,打开cmd窗口 4 输入网址 5注意要配置tomcat的 Application context为工程名字
- npm start a http server( 在windows的任意目录上开启一个http server 用来测试html 页面和js代码,不用放到nginx的webroot目录下!!)
原文:https://stackabuse.com/how-to-start-a-node-server-examples-with-the-most-popular-frameworks/#:~:t ...
- jupytext library using in jupyter notebook
目录 1. jupytext features 2. Way of using 3. usage 4. installation 1. jupytext features Jupytext can s ...
- Python win32gui调用窗口到最前面
Python win32gui调用窗口到最前面 0要写一个轮询几个重要页面的程序,不停的在大屏上进行刷新,通过pywin32模块下的SetForegroundWindow函数调用时,会出现error: ...
- 08 node.js 的使用
创建包 目录结构 cmd cd 到当前目录: \ 执行 npm init //创建一个包 1 2. 3. 4.包的安装 npm install jquery --save npm install ...
- work,工作模式
work,工作模式 一个消息只能被一个消费者获取 工作模式就是simple模式多了几个消费者,其他一样 来自为知笔记(Wiz)
- MySQL InnoDB存储引擎事务的ACID特性
1.前言 相信工作了一段时间的同学肯定都用过事务,也都听说过事务的4大特性ACID.ACID表示原子性.一致性.隔离性和持久性.一个很好的事务处理系统,必须具备这些标准特性: 原子性(Atomicit ...
- 看加载的php.ini 和 phpinfo 配置路径
php -i | grep "phar.readonly"看当前值php -i | grep "php.ini" 看加载的php.ini是哪个
- 深入基础(四)Buffer,转码
Buffer 前面提及到一些关于buffer类的问题,当时不是很明确 那么就次机会顺便深入探讨一下这个东西到底干嘛的出现在什么时候,如何使用.昨天跟朋友聊天他说我每一篇博文内容太长太长了 虽然 ...
- AtCoder Grand Contest 015题解
传送门 \(A\) 找到能达到的最大的和最小的,那么中间任意一个都可以被表示出来 typedef long long ll; int n,a,b;ll res; int main(){ scanf(& ...