当前位置:网站首页>【代码随想录-动态规划】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];
}
边栏推荐
- Deployment and operation of mongodb partitioned cluster
- 宝藏又小众的CTA动画素材素材网站分享
- Viteconfigure project path alias
- 【MATLAB项目实战】基于卷积神经网络与双向长短时(CNN-LSTM)融合的锂离子电池剩余使用寿命预测
- Programmer's essential toolkit, please collect!
- Technical scheme design of chain game system development - NFT chain game system development process and source code
- Calculate a=1, a2=1/1=a1
- Leetcode 1170. Frequency of occurrence of the minimum letter of the comparison string (yes, solved)
- Alibaba's "high concurrency" tutorial "basic + actual combat + source code + interview + Architecture" is a god class
- Count the number of each vowel letter in the string
猜你喜欢

Sandboxed container: container or virtual machine

Redis' 43 serial cannons, try how many you can carry

Synchronized description of concurrency
Teach you to learn dapr - 6 Publish subscription

Viewing the task arrangement ability of monorepo tool from turborepo

Notes on flowus

数字藏品与NFT到底有何区别

Byte interview: two array interview questions, please accept

SIGIR 2022 | 港大等提出超图对比学习在推荐系统中的应用

Leetcode HOT100 (22--- bracket generation)
随机推荐
Leetcode daily [2022 - 02 - 16]
Day10 daily 3 questions (3): String Matching in array
No manual prior is required! HKU & Tongji & lunarai & Kuangshi proposed self supervised visual representation learning based on semantic grouping, which significantly improved the tasks of target dete
Basic requirements: 7 problems in singleton mode
vue--vuerouter缓存路由组件
She said she was tired! So tired! I want to change my career
Use the array to calculate the average of N numbers, and output the numbers greater than the average
COMP5216 Mobile Computing Assignment 1 - Extending ToDoList app
The texstudio official website cannot be opened
Inspirational. In one year, from Xiaobai to entering the core Department of Alibaba, his counter attack
Record the use process of fenics
What is the preferential account opening policy of securities companies now? Is it safe to open an account online now?
Can Luo Yonghao succeed in entering the AR field this time?
ACL 2022 | 基于神经标签搜索的零样本多语言抽取式文本摘要
5g is not flat and 6G is restarted. China leads wireless communication. What is the biggest advantage of 6G?
建立自己的网站(16)
Troubleshooting ideas that can solve 80% of faults!
【推荐系统学习】推荐系统架构
Redis 概述整理
Science | giant bacteria found in mangroves challenge the traditional concept of nuclear free membrane