LeetCode 1143. Longest Common Subsequence
原题链接在这里:https://leetcode.com/problems/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.
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 Palindromic, Maximum Length of Repeated Subarray.
LeetCode 1143. Longest Common Subsequence的更多相关文章
- 【leetcode】1143. Longest Common Subsequence
题目如下: Given two strings text1 and text2, return the length of their longest common subsequence. A su ...
- 1143. Longest Common Subsequence
link to problem Description: Given two strings text1 and text2, return the length of their longest c ...
- Leetcode: Longest Common Subsequence
Given two strings text1 and text2, return the length of their longest common subsequence. A subseque ...
- 动态规划求最长公共子序列(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 ...
随机推荐
- 在CAD中插入谷歌地球卫星地图
本文主要介绍如何在CAD中插入谷歌地球卫星地图,作为参照光栅图像.谷歌地球卫星地图使用“迈高图-地图数据下载器”(以下简称:迈高图)下载.迈高图会给出相关插入参数(插入点和缩放比例),保证插入卫星地图 ...
- STVD生成hex,bin,显示ram&flash的使用情况
前言: 虽然stvd免费,但使用起来并不令人满意,不能自动补全,界面丑陋,设置繁琐,最难受的是不会自动输出ram和flash的使用情况.当然方法还是有的,下面就讲讲我是怎么实现的.个人水平有限,如有错 ...
- Golang 传递任意类型的切片
肯定有这样的一种场景,写一个函数,该函数可以接收任意类型的切片,完成相应的功能. 就好比这种情况 intSlice := []int{1,2,3,4,5,6,7,8} strSlice := []st ...
- Zookeeper的典型应用场景(转)
在寒假前,完成了Zookeeper系列的前5篇文章,主要是分布式的相关理论,包括CAP,BASE理论,分布式数据一致性算法:2PC,3PC,Paxos算法,Zookeeper的相关基本特性,ZAB协议 ...
- CSDN刷阅读数
今天我们来盘一下csdn,做一个小程序,为什么做这个呢?今天小编看着我的博客的阅读数,唉,惨不忍睹,没办法,只能想一些........呃呃呃呃,你懂的. 话不多说,分析一波csdn的阅读数,计数原理是 ...
- java之hiberante之集合映射之list映射
这篇讲解 集合映射之List映射 1.通常对于集合,在hibernate中的处理都是使用set来完成.但是hibernate也提供了对于其他几种集合的映射. 在这里实现List的映射,List是有序的 ...
- Mycat使用--分库分表和读写分离
Mycat分库分表读写分离 1. 模拟多数据库节点 2. 配置文件 具体操作参看: https://blog.csdn.net/vbirdbest/article/details/83448757 写 ...
- copy file
import io,,,,,,, from https://pub.dev/packages/large_file_copy Directory directory = await getApplic ...
- 2019-07-24 PHP中mysql_fetch_assoc 和 mysql_fetch_array 有什么区别?
mysql_fetch_assoc() 函数从结果集中取得一行作为关联数组 来看下面的例子: 数据库中有上述几条数据,一般我们想取用就要按照如下代码: $con = mysql_connect('12 ...
- Vue学习之npm常用命令及参数小结(十四)
NPM几个常用命令和参数的意思: npm install packagename 安装模块如不指定版本号 默认会安装最新的版本 npm install packagename 0.0.1 安装指定版本 ...