当前位置:网站首页>【代码随想录-动态规划】T583、两个字符串的删除操作
【代码随想录-动态规划】T583、两个字符串的删除操作
2022-06-26 16:56:00 【不写博客就不爽】
T583、两个字符串的删除操作
给定两个单词 word1 和 word2 ,返回使得 word1 和 word2 相同所需的最小步数。
每步 可以删除任意一个字符串中的一个字符
题解思路:
step1、dp[i][j] 表示 word1[0:i-1] 和 word2[0:j-1] 想要达到相等,所需要删除元素的最少次数。
step2、递推公式
当 word1.charAt(i-1) == word2.charAt(j-1) 时候,则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
第二种情况就要复杂一些,我写的时候只考虑到 dp[i][j-1]+1 这种情况,相当于只删除了 word2,所以最后提交 159/1396
public int minDistance(String word1, String word2) {
int n = word1.length();
int m = word2.length();
// 如何表示删除这个逻辑
// 上一道题目 dp[i][j]表示word1[:i-1]
// word2[:j-1]的最小步数
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];
}
边栏推荐
- 链游系统开发技术方案设计丨NFT链游系统开发流程及源码
- Introduction to minimal API
- 【万字总结】以终为始,详细分析高考志愿该怎么填
- Platform management background and merchant menu resource management: Design of platform management background data service
- 7 views on NFT market prospect
- Play with Linux and easily install and configure MySQL
- Alibaba's "high concurrency" tutorial "basic + actual combat + source code + interview + Architecture" is a god class
- [recommendation system learning] technology stack of recommendation system
- Teach you to learn dapr - 6 Publish subscription
- proxy
猜你喜欢

探讨:下一代稳定币

Ndroid development from introduction to mastery Chapter 2: view and ViewGroup

Alibaba's "high concurrency" tutorial "basic + actual combat + source code + interview + Architecture" is a god class

Wechat app mall, review products, upload commodity pictures, and score Commodity Services

Interpretation of cloud native microservice technology trend

Viewing the task arrangement ability of monorepo tool from turborepo

关于FlowUs这一款国民好笔记

Notes on flowus

5g is not flat and 6G is restarted. China leads wireless communication. What is the biggest advantage of 6G?

Leetcode 1170. 比较字符串最小字母出现频次(可以,已解决)
随机推荐
Science | giant bacteria found in mangroves challenge the traditional concept of nuclear free membrane
数字藏品与NFT到底有何区别
Memory partition model
SQL injection for Web Security (3)
Vue--vuerouter cache routing component
108. simple chat room 11: realize client group chat
探讨:下一代稳定币
SIGIR 2022 | University of Hong Kong and others proposed the application of hypergraph comparative learning in Recommendation System
Detailed contract quantification system development scheme and technical description of quantitative contract system development
Screenshot of the answers to C language exercises
Technical scheme design of chain game system development - NFT chain game system development process and source code
Cloud native 02: Alibaba cloud cloud efficient flow pipeline
Various types of gypsum PBR multi-channel mapping materials, please collect them quickly!
链游系统开发技术方案设计丨NFT链游系统开发流程及源码
NFT 交易市场社区所有化势不可挡
Teach you to learn dapr - 2 Must know concept
Viteconfigure project path alias
5g is not flat and 6G is restarted. China leads wireless communication. What is the biggest advantage of 6G?
Teach you to learn dapr - 4 Service invocation
What is the difference between digital collections and NFT