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 ...
随机推荐
- php mysql的储存过程 实现批量更新
UPDATE newhouse_clicks SET clicks = CASE id WHEN 1 THEN 1000 WHEN 2 THEN 2000 WHEN 3 THEN 3000 END W ...
- Storm大数据实时计算
大数据也是构建各类系统的时候一种全新的思维,以及架构理念,比如Storm,Hive,Spark,ZooKeeper,HBase,Elasticsearch,等等 storm,在做热数据这块,如果要做复 ...
- Kubernetes(K8s)基础知识(docker容器技术)
今天谈谈K8s基础知识关键词: 一个目标:容器操作:两地三中心:四层服务发现:五种Pod共享资源:六个CNI常用插件:七层负载均衡:八种隔离维度:九个网络模型原则:十类IP地址:百级产品线:千级物理机 ...
- MongoDB和Java(7):MongoDB用户管理
最近花了一些时间学习了下MongoDB数据库,感觉还是比较全面系统的,涉及了软件安装.客户端操作.安全认证.副本集和分布式集群搭建,以及使用Spring Data连接MongoDB进行数据操作,收获很 ...
- 执行kubectl create-f replicaset.yaml后k8s是如何工作的
参考:提高 kubectl 使用生产力[译] 英文原文:https://learnk8s.io/blog/kubectl-productivity/ Kubernetes 架构 Kubernetes ...
- Kubernetes Storage Persistent Volumes
链接:https://kubernetes.io/docs/concepts/storage/persistent-volumes/ 支持的参数,比如mountOptions在这里可以找到 删除正在被 ...
- IDEA插件:search with bing、search with baidu
//转载请注明出处:https://www.cnblogs.com/nreg/p/11267169.html 当项目出现错误时,经常需要复制错误信息粘贴到浏览器查询,但是手动复制再粘贴太麻烦了, 因此 ...
- window.requestAnimationFrame()的使用,处理更流畅的动画效果
https://blog.csdn.net/w2765006513/article/details/53843169 window.requestAnimationFrame()的使用 2016年12 ...
- 约束布局ConstraintLayout
Android新特性介绍,ConstraintLayout完全解析 约束布局ConstraintLayout用法全解析 约束布局ConstraintLayout看这一篇就够了
- Telnet入侵WindowsXP
上一章,采用图形界面配置.这一节,采用命令方式配置 //修复.bat(掩饰名字) @ echo off //关闭回显 regedit.exe /s start.reg ///s 不打印 net sta ...