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 <= 10001 <= 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 ...
随机推荐
- Spring Boot与mybatis整合
完整的项目截图 一:pom依赖 新增ojdbc6及batis-spring-boot-starter依赖 <dependency> <groupId>com.oracle< ...
- Idea中类实现Serializable接口 引入 serialVersionUID
idea实现Serializable接口,然后打出serialVersionUID的办法 setting>editor>Inspection>Java>Serializatio ...
- eclipse创建springboot项目的三种方法
本文链接:https://blog.csdn.net/mousede/article/details/81285693 方法一 安装STS插件 安装插件导向窗口完成后,在eclipse右下角将会出现安 ...
- 自学Python编程的第二天----------来自苦逼的转行人
今天是2019.9.11号22:51分 这是我自学Python的第二天,也是我写博客的第二天,还是不知道怎样写博客的第二天,有点懵 今天学Python还是一样的懵,错误还是有很多,而且脑中也不够灵活, ...
- permission
import 'package:flutter/material.dart'; import 'dart:io'; import 'dart:async'; import 'package:rxdar ...
- Stack布局中定位的方式
//……省略无关代码…… child: new Column( children: <Widget>[ new SizedBox(height: 20.0), new Stack( ali ...
- Matlab观察者模式
要点: 1.服务端(Subject)维护一个观察者的列表,以便能够向所有的观察者(Observer)推送信息 2.观察者可以获取服务端的状态 3.服务端和观察者可抽象,可以有多个不同实现 Subjec ...
- JavaScript原型链以及Object,Function之间的关系
JavaScript里任何东西都是对象,任何一个对象内部都有另一个对象叫__proto__,即原型,它可以包含任何东西让对象继承.当然__proto__本身也是一个对象,它自己也有自己的__proto ...
- Linux下which、whereis、locate、find命令作用
1 which 查看可执行文件的位置,也可以找到命令别名 2 whereis 查看文件的位置 3 locate 系统数据库查找文件位置,数据库大约每天更新一次 4 find 根据查找条件,搜寻硬盘查询 ...
- CentOS 下运行.net Core程序
系统: 阿里云的默认 CentOS 7.5 镜像 项目环境:.Net Core 2.2 一.安装.Net Core的运行环境 第一步,如果是一台新的服务器,可以升级一下系统的基础软件.如果没有必要也可 ...