当前位置:网站首页>[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];
}
边栏推荐
- 20: Chapter 3: develop the pass service: 3: get through the redis server in the program; (it only connects with the redis server and does not involve specific business development)
- 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
- halcon之区域:多种区域(Region)特征(5)
- Romance of the Three Kingdoms: responsibility chain model
- Ndroid development from introduction to mastery Chapter 2: view and ViewGroup
- Use the array to calculate the average of N numbers, and output the numbers greater than the average
- MySql 导出数据库中的全部表索引
- Apache APIs IX has the risk of rewriting the x-real-ip header (cve-2022-24112)
- In those years, interview the abused red and black trees
- Microservice architecture practice: user login and account switching design, order query design of the mall
猜你喜欢

Redis and database data consistency

经典同步问题

10 cloud security best practices that enterprises need to know

Synchronized description of concurrency

14《MySQL 教程》INSERT 插入数据

Implementation of MySQL master-slave architecture

sparksql如何通过日期返回具体周几-dayofweek函数

NFT 交易市场社区所有化势不可挡

直播预告|程序员进击,如何提升研发效能?6月21日晚视频号、B站同步直播,不见不散!

Introduction to distributed cache / cache cluster
随机推荐
Leetcode 1170. 比较字符串最小字母出现频次(可以,已解决)
并发之Synchronized说明
10 cloud security best practices that enterprises need to know
Viteconfigure project path alias
Troubleshooting ideas that can solve 80% of faults!
num[i]++
Technical scheme design of chain game system development - NFT chain game system development process and source code
【uniapp】uniapp手机端使用uni.navigateBack失效问题解决
Use the array to calculate the average of N numbers, and output the numbers greater than the average
The function keeps the value of variable H to two decimal places and rounds the third digit
Teach you to learn dapr - 1 The era of net developers
【万字总结】以终为始,详细分析高考志愿该怎么填
Ndroid development from introduction to mastery Chapter 2: view and ViewGroup
SIGIR 2022 | 港大等提出超图对比学习在推荐系统中的应用
Uncover the secret of Agora lipsync Technology: driving portraits to simulate human speech through real-time voice
数字藏品与NFT到底有何区别
Leetcode daily [2022 - 02 - 16]
Redis OM . Net redis object mapping framework
Platform management background and merchant menu resource management: Design of platform management background data service
宝藏又小众的CTA动画素材素材网站分享