当前位置:网站首页>【代码随想录-动态规划】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];
}
边栏推荐
- Teach you to learn dapr - 2 Must know concept
- Don't believe it, 98% of programmers are like this
- She said she was tired! So tired! I want to change my career
- Cache breakdown! Don't even know how to write code???
- Demonstrate to Xiaobai the case of sub database and sub table
- Over the weekend: 20000 words! Summary of JVM core knowledge, 18 serial cannons as a gift
- SIGIR 2022 | 港大等提出超图对比学习在推荐系统中的应用
- Screenshot of the answers to C language exercises
- Viewing the task arrangement ability of monorepo tool from turborepo
- 分布式架构概述
猜你喜欢
Decentralized NFT transaction protocol will defeat opensea
Don't believe it, 98% of programmers are like this
A simple membership card management system based on Scala
防火 疏散 自救…这场安全生产暨消防培训干货满满!
Leetcode 1170. 比较字符串最小字母出现频次(可以,已解决)
进军AR领域,这一次罗永浩能成吗?
关于FlowUs这一款国民好笔记
MS | Xie Liwei group found that mixed probiotics and their metabolites could alleviate colitis
Deployment and operation of mongodb partitioned cluster
In those years, interview the abused red and black trees
随机推荐
Army chat -- registration of Registration Center
[latex bearer] use tables in \title (error \begin doesn't match its definition.)
Synchronized description of concurrency
Interpretation of cloud native microservice technology trend
Technical scheme design of chain game system development - NFT chain game system development process and source code
直播预告|程序员进击,如何提升研发效能?6月21日晚视频号、B站同步直播,不见不散!
Treasure and niche CTA animation material website sharing
Programmer interview guide - self introduction
7 views on NFT market prospect
Experience in hierarchical debugging of boards and cards
Find all primes less than or equal to Lim, store them in AA array, and return the number of primes
Find out the maximum value of each column element of NxN matrix and store it in the one-dimensional array indicated by formal parameter B in order
What does the equals method compare? Who told you
Redis 概述整理
The texstudio official website cannot be opened
Day10 daily 3 questions (1): sum gradually to get the minimum value of positive numbers
MySQL index
Redis and database data consistency
Don't believe it, 98% of programmers are like this
Platform management background and merchant menu resource management: merchant registration management design