当前位置:网站首页>1143 longest common subsequence
1143 longest common subsequence
2022-04-21 23:29:00 【yoyooyooo】
Description
Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0.
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.
For example, “ace” is a subsequence of “abcde”.
A common subsequence of two strings is a subsequence that is common to both strings.
Examples
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, text2.length <= 1000
text1 and text2 consist of only lowercase English characters.
Ideas
dp, Construct a 2 D matrix ,dp[i][j] Express text1.substring(0, i + 1) and text2.substring(0, j + 1) There are several identical elements , This update dp[i][j] The state transition formula is
- If
text1.charAt(i) == text1.charAt(j)——dp[i][j] = dp[i-1][j-1] + 1 - The second step , perform
dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1], dp[i][j])
Code
class Solution {
public int longestCommonSubsequence(String text1, String text2) {
int[][] dp = new int[text1.length() + 1][text2.length() + 1];
for (int i = 0; i < text1.length(); i++) {
for (int j = 0; j < text2.length(); j++) {
int add = text1.charAt(i) == text2.charAt(j)? 1: 0;
dp[i + 1][j + 1] = dp[i][j] + add;
dp[i + 1][j + 1] = Math.max(dp[i][j + 1], dp[i + 1][j + 1]);
dp[i + 1][j + 1] = Math.max(dp[i + 1][j], dp[i + 1][j + 1]);
}
}
return dp[text1.length()][text2.length()];
}
}
版权声明
本文为[yoyooyooo]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204212327216442.html
边栏推荐
- 2022/4/21
- 技术、产品、品牌都不是问题,对上汽奥迪而言,这2点或生死攸关
- 系列文章分类汇总(第二期)
- Qt自定义控件01 简易计时器
- leetcode:271. Encoding and decoding of strings
- File operation and IO
- leetcode:440. The k-th smallest digit in dictionary order
- Why don't MySQL use select * as query criteria? (continuously updated)
- 瑞芯微芯片AI部分开发记录 第一节 《PC端环境搭建2》
- golang力扣leetcode 2246.相邻字符不同的最长路径
猜你喜欢

Click the imported file or click the component to enter the corresponding component page for editing

GIC spec之ITS和LPI中断5

leetcode:386. Dictionary order

从零开始自制实现WebServer(十六)---- 学习新工具CMake自动编写MakeFile 分门别类整理源文件心情愉悦

【速卖通代运营】2022跨境电商怎么做?速卖通今年重点要做三件事
![[wrapper (1)]](/img/78/362594cbf940d3ab89e6d9d8891a5e.png)
[wrapper (1)]

BUUCTF 你(竟)然赶我走

LeetCode_ 70 climb stairs

Selection and evolution of microservices under cloud native architecture

【acwing】1125. Cattle travel * * * (Floyd)
随机推荐
PP semantic retrieval system
入参有汉字,报错500,服务器内部错误
简约易收录的导航网站源码
golang力扣leetcode 380.O(1)时间插入、删除和获取随机元素
PP语义检索系统
5. Network structure and ISP, packet delay, loss, throughput
339 leetcode word rules
L2-016 愿天下有情人都是失散多年的兄妹 (25 分)
自定义模板问题求助,自动添加时间日期
idea 解决项目包出现[wrapper(1)]
Golang force buckle leetcode 479 Maximum palindrome product
通过点击导入的文件或点击组件进入对应的组件页面进行编辑
Two benchmark performance test methods of redis
JMeter association parameters
Buuctf, you drove me away
Click the imported file or click the component to enter the corresponding component page for editing
BUUCTF 刷题记录
Developing Cami community system with ThinkPHP
QT custom control 01 simple timer
Simple and easy to collect navigation website source code