当前位置:网站首页>Sword finger offer II 095 Longest common subsequence
Sword finger offer II 095 Longest common subsequence
2022-06-25 06:17:00 【Normal male human】
Given two strings text1 and text2, Returns the longest of these two strings Common subsequence The length of . If it doesn't exist Common subsequence , return 0 .
A string of Subsequence This is a new string : It is to delete some characters from the original string without changing the relative order of the characters ( You can also delete no characters ) After the composition of the new string .
for example ,“ace” yes “abcde” The subsequence , but “aec” No “abcde” The subsequence .
Two strings of Common subsequence Is the subsequence shared by these two strings .
Example 1:
Input :text1 = “abcde”, text2 = “ace”
Output :3
explain : The longest common subsequence is “ace” , Its length is 3 .
Example 2:
Input :text1 = “abc”, text2 = “abc”
Output :3
explain : The longest common subsequence is “abc” , Its length is 3 .
Example 3:
Input :text1 = “abc”, text2 = “def”
Output :0
explain : The two strings have no common subsequence , return 0 .
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/qJnOS7
Dynamic programming :
class Solution {
public int longestCommonSubsequence(String text1, String text2) {
int m = text1.length(), n = text2.length();
int[][] dp = new int[m + 1][n + 1];
for (int i = 1; i <= m; i++) {
char c1 = text1.charAt(i - 1);
for (int j = 1; j <= n; j++) {
char c2 = text2.charAt(j - 1);
if (c1 == c2) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[m][n];
}
}
dp[i][j] The meaning of :text1 Of 0 ~ i String and text2 Of 0 ~ j The common subsequence length of the string of .
First, we can determine the boundary, that is dp[i][0] and dp[0][i] = 0, Because the length is 0 The length of the common subsequence of the string must also be 0.
Because array initialization is 0, So you don't have to initialize the boundary again .
When subscript i And j Situated The letters are the same , explain dp[i][j] = dp[i-1][j-1] + 1,+1 Refer to subscript i and j Can be directly added to the longest common subsequence .
If the letters are different , be Compare dp[i-1][j] and dp[i][j-1] In both cases , Take the larger .
From a two-dimensional perspective , Whether it's dp[i-1][j] still dp[i][j-1] All in dp[i][j] Top left of , So we have i It is an external heavy cycle j Traverse for inner loop , You can finish the whole dp The calculation of array .
Eventually return dp[m][n].
边栏推荐
- Technology inventory: Technology Evolution and Future Trend Outlook of cloud native Middleware
- TFTP command – uploading and downloading files
- Global and China chemical mechanical polishing abrasive materials market demand outlook and investment scale forecast report 2022 Edition
- Fdisk command – disk partition
- [interview with a large factory] meituan had two meetings. Was there a surprise in the end?
- Mongodb basic concept learning - set
- Advantages and disadvantages of using SNMP and WMI polling
- Guess the size of the number
- Lesson 9: workspace introduction
- What is the slice flag bit
猜你喜欢

Soft exam information system project manager_ Information system security management - Senior Information System Project Manager of soft test 026

CTFSHOW

50 days countdown! Are you ready for the Landbridge cup provincial tournament?
SAP ui5 beginner tutorial No. 27 - unit test tool quNit introduction trial version for SAP ui5 application
SAP ui5 Application Development Tutorial Part 30 - parameter transfer in the routing process of SAP ui5
Some common errors and solutions of using SAP ui5 to consume OData services

What happens when redis runs out of memory

Monitoring access: how to grant minimum WMI access to the monitoring service account

Mongodb delete data

Es11 new methods: dynamic import(), bigint, globalthis, optional chain, and null value merging operator
随机推荐
Gavin's insight on transformer live class - line by line analysis and field experiment analysis of insurance BOT microservice code of insurance industry in the actual combat of Rasa dialogue robot pro
The sum problem
CST8227
DF command – displays disk space usage
cacacahe
A + B Again
Noi Mathematics: Dirichlet convolution
How the sap ui5 framework performs single step debugging of batch requests
Mongodb delete data
50 days countdown! Are you ready for the Landbridge cup provincial tournament?
What is the slice flag bit
Notes on dashboard & kuboard installation in kubernetes cluster
Research Report on marketing channel analysis and competitive strategy of China's polycarbonate industry 2022
Copying DNA
DOM proficient? What is the difference between node and elment?
Talk about TCP and UDP
[golang] leetcode intermediate - Search rotation sort array & search two-dimensional matrix II
Ping command – test network connectivity between hosts
You can't specify target table for update in from clause error in MySQL
Research Report on global and Chinese vaccine market profit forecast and the 14th five year plan development strategy 2022-2028