当前位置:网站首页>[code Capriccio - dynamic planning] t583. Deleting two strings
[code Capriccio - dynamic planning] t583. Deleting two strings
2022-06-26 17:18:00 【Not blogging】
T583、 Deletion of two strings
Give two words word1 and word2 , Return makes word1 and word2 The same minimum number of steps required .
Every step You can delete a character from any string
Solution to the problem :
step1、dp[i][j] Express word1[0:i-1] and word2[0:j-1] To achieve equality , The minimum number of times an element needs to be deleted .
step2、 The recursive formula
When word1.charAt(i-1) == word2.charAt(j-1) When , be dp[i][j] = dp[i-1][j-1]
https://www.programmercarl.com/0583.%E4%B8%A4%E4%B8%AA%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E5%88%A0%E9%99%A4%E6%93%8D%E4%BD%9C.html
The second case is more complicated , When I write, I only consider dp[i][j-1]+1 This situation , It is equivalent to deleting only word2, So finally submit 159/1396
public int minDistance(String word1, String word2) {
int n = word1.length();
int m = word2.length();
// How to delete this logic
// Last question dp[i][j] Express word1[:i-1]
// word2[:j-1] The minimum number of steps
int[][] dp = new int[n+1][m+1];
for(int j = 0; j <= m; j++){
dp[0][j] = j;
}
for(int i = 0; i <= n; i++){
dp[i][0] = i;
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(word1.charAt(i-1) == word2.charAt(j-1)){
dp[i][j] = dp[i-1][j-1];
}else{
dp[i][j] = Math.min(dp[i-1][j-1]+2, Math.min(dp[i][j-1] + 1, dp[i-1][j]+1));
}
}
}
return dp[n][m];
}
边栏推荐
- Byte interview: two array interview questions, please accept
- Decentralized NFT transaction protocol will defeat opensea
- 并发之线程安全
- Count the number of words in a line of string and take it as the return value of the function
- Comp281 explanation
- 丰富专业化产品线, 江铃福特领睿·极境版上市
- Leetcode 1170. 比较字符串最小字母出现频次(可以,已解决)
- 一起备战蓝桥杯与CCF-CSP之大模拟炉石传说
- sql中ROUND和TRUNCATE的区别(四舍五入还是截取小数点后几位)
- Teach you to learn dapr - 3 Run the first with dapr Net program
猜你喜欢
Leetcode 1170. Frequency of occurrence of the minimum letter of the comparison string (yes, solved)
Microservice architecture practice: business management background and SSO design: SSO design
防火 疏散 自救…这场安全生产暨消防培训干货满满!
【万字总结】以终为始,详细分析高考志愿该怎么填
Byte interview: two array interview questions, please accept
有依赖的背包问题
背包问题求方案数
Programmer's essential toolkit, please collect!
sparksql如何通过日期返回具体周几-dayofweek函数
The latest masterpiece of Alibaba, which took 182 days to produce 1015 pages of distributed full stack manual, is so delicious
随机推荐
去中心化NFT交易协议将击败OpenSea
Technical scheme design of chain game system development - NFT chain game system development process and source code
Multiply the values of the upper triangular elements of the array by M
Leetcode 1170. Frequency of occurrence of the minimum letter of the comparison string (yes, solved)
[qt learning notes]qt inter thread data communication and data sharing
Leetcode 1169. 查询无效交易(如果数据量不大,这种题还是得暴力枚举解决)
Fgetc() reads content from file
Apache APIs IX has the risk of rewriting the x-real-ip header (cve-2022-24112)
Deployment and operation of mongodb partitioned cluster
Turtle cartography
Microservice architecture practice: user login and account switching design, order query design of the mall
ACL 2022 | 基于神经标签搜索的零样本多语言抽取式文本摘要
Find all primes less than or equal to Lim, store them in AA array, and return the number of primes
Calculate the average of N numbers in the group indexed by the formal parameter x, move the data less than the average in the group indexed to the front of the array, and move the data greater than or
Uncover the secret of Agora lipsync Technology: driving portraits to simulate human speech through real-time voice
COMP5216 Mobile Computing Assignment 1 - Extending ToDoList app
Use the array to calculate the average of N numbers, and output the numbers greater than the average
Teach you to learn dapr - 4 Service invocation
14《MySQL 教程》INSERT 插入数据
探讨:下一代稳定币