当前位置:网站首页>LeetCode 583. Deleting two strings
LeetCode 583. Deleting two strings
2022-06-13 09:21:00 【Shao_ sen】
583. Deletion of two strings
difficulty secondary
Give two words word1 and word2 , Return makes word1 and word2 identical The required The minimum number of steps .
Every step You can delete a character from any string .
Example 1:
Input : word1 = "sea", word2 = "eat"
Output : 2
explain : The first step will be "sea" Turn into "ea" , The second step will be "eat " Turn into "ea"
Example 2:
Input :word1 = "leetcode", word2 = "etco"
Output :4
Tips :
1 <= word1.length, word2.length <= 500word1andword2Only lowercase letters
Answer key
This problem lets us find the minimum number of steps for bidirectional conversion of two strings , Because I have done the title of the longest common subsequence , So it's easy to figure out how to solve , Is to find the common subsequence of two characters , Then the other characters of the common subsequence are deleted from both strings , That's the final result .
- First find the length of the longest common subsequence
- Then subtract the length of the longest common subsequence from the length of the two strings
class Solution {
public int minDistance(String word1, String word2) {
int l1 = word1.length();//word1 length
int l2 = word2.length();//word2 length
int[][] dp = new int[l1 + 1][l2 + 1];// The dynamic array
for(int i = 1; i <= l1; i++){
for(int j = 1; j <= l2; j++){
if(word1.charAt(i -1) == word2.charAt(j - 1)){
// Same letter , The length of the longest common subsequence plus one ( In state i-1,j-1 Add one to the case of )
dp[i][j] = dp[i - 1][j - 1] + 1;
}else{
// The letters are different , Take the State i-1,j Or state i,j-1 Smallest of
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return l1 + l2 - 2 * dp[l1][l2];// Subtract the length of the longest common subsequence from the length of the two strings
}
}
边栏推荐
猜你喜欢

Solov2 nanny level tutorial (including environment configuration, training your own data set, code logic analysis, etc...) Updating ing

Tutorial (5.0) 04 Fortint cloud services and scripts * fortiedr * Fortinet network security expert NSE 5

CAS NO lock

202012 CCF test questions

Routing - static routing

Neo4j - CQL use

Simulink variant model and variant subsystem usage

C/s model and P2P model

Some websites of QT (software download, help documents, etc.)

Overview of common layers of image recognition neural network (under update)
随机推荐
Class loading overview
20211104 why is the trace of a matrix equal to the sum of eigenvalues, and why is the determinant of a matrix equal to the product of eigenvalues
Alibaba senior experts analyze the standard design of protocol
LeetCode 5259. Calculate the total tax payable
20211018 some special matrices
攻防世界-PWN-shell
20211028 adjustment and tracking
Delete soft link
JUC Unsafe
How to build an aby framework and run an instance
Neo4j环境搭建
I set up a blog
Lecture par lots de tous les fichiers vocaux sous le dossier
共享模型之不可变
20211115 any n-order square matrix is similar to triangular matrix (upper triangle or lower triangle)
Solov2 source code analysis
Online debugging tool Arthas advanced
20220524 how to install coppeliasim to disk D
Two good kids
LeetCode 6095. 强密码检验器 II