此题链接:Delete Operation for Two Strings - LeetCode
LeetCode 动态规划(Dynamic programming)系列题目:LeetCode 动态规划(Dynamic programming)系列题目
升级版题目:Edit Distance - LeetCode
文章地址:LeetCode 72. Edit Distance - zhangpeterx的博客 - CSDN博客


Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in each step you can delete one character in either string.

Example 1:

Input: "sea", "eat"
Output: 2

Explanation: You need one step to make “sea” to “ea” and another step to make “eat” to “ea”.
Note:

  • The length of given words won’t exceed 500.
  • Characters in given words can only be lower-case letters.

这道题目是求最长公共子串,最容易想到的就是动态规划的解法。

Java解法如下:


public class Solution {public int minDistance(String s1, String s2) {int[][] memo = new int[s1.length() + 1][s2.length() + 1];return s1.length() + s2.length() - 2 * lcs(s1, s2, s1.length(), s2.length(), memo);}public int lcs(String s1, String s2, int m, int n, int[][] memo) {if (m == 0 || n == 0)return 0;if (memo[m][n] > 0)return memo[m][n];if (s1.charAt(m - 1) == s2.charAt(n - 1))memo[m][n] = 1 + lcs(s1, s2, m - 1, n - 1, memo);elsememo[m][n] = Math.max(lcs(s1, s2, m, n - 1, memo), lcs(s1, s2, m - 1, n, memo));return memo[m][n];}
}

这个解法里面重复计算了许多次,可以进行优化。
上面的做法使用了递归,很慢,使用迭代会快非常多。

public class Solution {public int minDistance(String s1, String s2) {int[][] dp = new int[s1.length() + 1][s2.length() + 1];for (int i = 0; i <= s1.length(); i++) {for (int j = 0; j <= s2.length(); j++) {if (i == 0 || j == 0)continue;if (s1.charAt(i - 1) == s2.charAt(j - 1))dp[i][j] = 1 + dp[i - 1][j - 1];elsedp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);}}return s1.length() + s2.length() - 2 * dp[s1.length()][s2.length()];}
}

这种解法也有缺点,

Ark-kun: Most DP solutions can be optimized. Instead of calculating all matrix, we actually only need the area with edit distance <= result. Suppose, you have 2 equal strings with length 1 million. With the described approach, you’ll process all 1000000000000 cells, while my A* path-finding approach will just go diagonally straight from corner to corner. The listed DP approaches are always O(n*m); my approach can be as fast as max(n,m).


Python解法如下:

class Solution:def minDistance(self, word1: str, word2: str) -> int:m, n = len(word1), len(word2)dp = [[0] * (n + 1) for i in range(m + 1)]for i in range(m):for j in range(n):dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j], dp[i][j] + (word1[i] == word2[j]))return m + n - 2 * dp[m][n]

C++解法如下:

class Solution {public:int minDistance(string a, string b) {int m = a.size(), n = b.size();vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));for (int i = m; i >= 0; i--) {for (int j = n; j >= 0; j--) {if (i < m || j < n)dp[i][j] = i < m && j < n && a[i] == b[j] ?dp[i + 1][j + 1] : 1 + min((i < m ? dp[i + 1][j] : INT_MAX), (j < n ? dp[i][j + 1] : INT_MAX));}}return dp[0][0];}
};

LeetCode 583. Delete Operation for Two Strings--动态规划 DP--Java,Python,C++解法相关推荐

  1. leetcode 583. Delete Operation for Two Strings | 583. 两个字符串的删除操作(最长公共子序列,DP)

    题目 https://leetcode.com/problems/delete-operation-for-two-strings/ 题解 本题实质上是个最长公共子序列问题,又是经典的 递归-> ...

  2. leetcode 279. Perfect Squares | 279. 完全平方数(动态规划,Java)

    题目 https://leetcode.com/problems/perfect-squares/ 题解:动态规划 参考:[宫水三叶]详解完全背包一维空间优化推导(附背包问题攻略) 首先初始化长度为 ...

  3. leetcode 58. Length of Last Word 题解【C++/Java/Python/JS】

    58. 最后一个单词的长度 58. Length of Last Word 题目: 给定一个仅包含大小写字母和空格' ' 的字符串,返回其最后一个单词的长度. 如果不存在最后一个单词,请返回0 . 说 ...

  4. LeetCode 712. Minimum ASCII Delete Sum for Two Strings

    712.Minimum ASCII Delete Sum for Two Strings(两个字符串的最小ASCII删除和) 题目: 给定两个字符串s1, s2,找到使两个字符串相等所需删除字符的AS ...

  5. 【LeetCode】1641. Count Sorted Vowel Strings(动态规划)

    [LeetCode]1641. Count Sorted Vowel Strings(动态规划) Given an integer n, return the number of strings of ...

  6. LeetCode 583. 两个字符串的删除操作(动态规划)

    1. 题目 给定两个单词 word1 和 word2,找到使得 word1 和 word2 相同所需的最小步数,每步可以删除任意一个字符串中的一个字符. 示例: 输入: "sea" ...

  7. LeetCode 1143. 最长公共子序列(动态规划)

    1. 题目 给定两个字符串 text1 和 text2,返回这两个字符串的最长公共子序列的长度. 一个字符串的 子序列 是指这样一个新的字符串:它是由原字符串在不改变字符的相对顺序的情况下删除某些字符 ...

  8. LeetCode 552. 学生出勤记录 II(动态规划)

    文章目录 1. 题目 2. 解题 1. 题目 给定一个正整数 n,返回长度为 n 的所有可被视为可奖励的出勤记录的数量. 答案可能非常大,你只需返回结果mod 10^9 + 7的值. 学生出勤记录是只 ...

  9. LeetCode 576. 出界的路径数(动态规划)

    文章目录 1. 题目 2. 解题 1. 题目 给定一个 m × n 的网格和一个球. 球的起始坐标为 (i,j) ,你可以将球移到相邻的单元格内,或者往上.下.左.右四个方向上移动使球穿过网格边界. ...

最新文章

  1. 新建ROS工作工作空间
  2. 开源教程 「nlp-tutorial」!用百行代码搞定各类NLP模型
  3. scala成长之路(2)对象和类
  4. 2.4.3 死锁的处理策略-避免死锁
  5. 如何用C语言编写PHP扩展的详解
  6. 将解决方案和项目放在同一目录中_借助CADENAS 3D电子目录,巴鲁夫使用CAE数据扩展其产品目录...
  7. Apache Lens —— 统计数据分析查询接口
  8. mysql怎么从1开始递增
  9. string转成对象_非常简单的string驻留池,你对它真的了解吗
  10. NPDP第五章 工具与度量
  11. STM32L4系列单片机如何使用RTC唤醒定时器进入Standby低功耗模式并唤醒+整机功耗测试
  12. 数据结构(二十) -- C语言版 -- 树 - 霍夫曼树(哈夫曼树、赫夫曼树、最优二叉树)、霍夫曼编码
  13. web前端项目 - cypress自动化测试运行构建
  14. 【小Game】C++ - EGE - 躲避球小游戏
  15. iphone13电话噪音大怎么办 苹果13怎么设置电话降噪
  16. 免费的配音软件有哪些?手机上就能操作的
  17. python你已经是个成熟的软件了_你已经是个成熟的系列表情包大全_支付宝微信等软件中招_软吧...
  18. 用 Neo4j 快速构建明星关系图谱,你一定感兴趣
  19. 新浪微博和腾讯微博图标
  20. 刷脸支付到来用户连密码都不需要

热门文章

  1. CentOS 7 源码编译安装 PostgreSQL 11.2
  2. RDKit:化合物相似性搜索
  3. conda安装qiime2-清华镜像源替换法解决安装失败
  4. 合种侧柏、云杉专车2-3天领证
  5. 中国科学7月微生物组专刊:赵立平、秦楠、东秀珠领衔
  6. 我们从那里来—子宫日记 Womb
  7. R语言为dataframe添加新的数据列(横向拼接、Appending columns,Unioning columns):使用R原生方法、data.table、dplyr等方案
  8. pandas读取多个文件内容为dataframe、并合并为一个dataframe、pandas创建仅有列标签而内容为空的dataframe
  9. R语言原生hist函数绘制直方图实战
  10. R语言dim函数返回NULL