【LeetCode】583. Delete Operation for Two Strings 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/delete-operation-for-two-strings/description/
题目描述
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.
题目大意
给出了两个字符串,可以删除两个字符串中的某些字符,求最少删除多少个字符之后两个字符串相等。
解题方法
一直觉得这一个题非常的难,所以就没做。昨天复习了一下机试指南之后,发现这个题不就是求最长公共子序列(LCS)吗?顿时豁然开朗。LCS和LIS一样是经典的动态规划问题应该背会的。这个题在机试指南的162页。
但是,别忘了一点,求出LCS后还要用两者的长度之和减去LCS的长度,才是我们应该删除的字符长度。
如果还不是很明白,可以看这个题的官方解答:https://leetcode.com/articles/delete-operation-for-two-strings/,有dp的二维数组,能把变化看的非常清楚。
代码:
class Solution(object):
def minDistance(self, word1, word2):
"""
:type word1: str
:type word2: str
:rtype: int
"""
_len1, _len2 = len(word1), len(word2)
dp = [[0] * (len(word2) + 1) for _ in range(len(word1) + 1)]
for i in range(1, len(word1) + 1):
for j in range(1, len(word2) + 1):
if word1[i-1] == word2[j-1]:
dp[i][j] = dp[i-1][j-1] + 1
else:
dp[i][j] = max(dp[i][j-1], dp[i-1][j])
val = dp[-1][-1]
return _len1 - val + _len2 - val
二刷使用的C++,发现和上面的做法有点区别。上面的做法是最长公共子序列,这个题的做法可以指直接使用要删除的序列。
这个题的做法和712. Minimum ASCII Delete Sum for Two Strings很像,代码如下:
class Solution {
public:
int minDistance(string word1, string word2) {
const int M = word1.size(), N = word2.size();
vector<vector<int>> dp(M + 1, vector<int>(N + 1, 0));
for (int i = 1; i < M + 1; i ++)
dp[i][0] = dp[i - 1][0] + 1;
for (int j = 1; j < N + 1; j ++)
dp[0][j] = dp[0][j - 1] + 1;
for (int i = 1; i < M + 1; i ++) {
for (int j = 1; j < N + 1; j ++) {
if (word1[i - 1] == word2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + 1;
}
}
}
return dp[M][N];
}
};
日期
2018 年 4 月 4 日 —— 清明时节雪纷纷~~下雪了,惊不惊喜,意不意外?
2018 年 12 月 14 日 —— 12月过半,2019就要开始
【LeetCode】583. Delete Operation for Two Strings 解题报告(Python & C++)的更多相关文章
- [LeetCode] 583. Delete Operation for Two Strings 两个字符串的删除操作
Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...
- LeetCode 583 Delete Operation for Two Strings 删除两个字符串的不同部分使两个字符串相同,求删除的步数
Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...
- 【Leetcode】583. Delete Operation for Two Strings
583. Delete Operation for Two Strings Given two words word1 and word2, find the minimum number of st ...
- 【LeetCode】712. Minimum ASCII Delete Sum for Two Strings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LC 583. Delete Operation for Two Strings
Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...
- 583. Delete Operation for Two Strings
Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...
- 【LeetCode】893. Groups of Special-Equivalent Strings 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】450. Delete Node in a BST 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 日期 题目地址:https://leetcode ...
- LeetCode 944 Delete Columns to Make Sorted 解题报告
题目要求 We are given an array A of N lowercase letter strings, all of the same length. Now, we may choo ...
随机推荐
- SQL-Union、Union ALL合并两个或多个 SELECT 语句的结果集
UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每条 SELECT 语句中的列 ...
- 2 — springboot的原理
1.初步探索:第一个原理:依赖管理 发现:这里面存放着各种jar包 和 版本号 这也是:我们在前面第一个springboot项目创建中勾选了那个web,然后springboot就自动帮我们导入很多东西 ...
- nodeJs-process对象
JavaScript 标准参考教程(alpha) 草稿二:Node.js process对象 GitHub TOP process对象 来自<JavaScript 标准参考教程(alpha)&g ...
- 容器之分类与各种测试(三)——stack
stack是栈,其实现也是使用了双端队列(只要不用双端队列的一端,仅用单端数据进出即完成单端队列的功能),由于queue和stack的实现均是使用deque,没有自己的数据结构和算法,所以这俩也被称为 ...
- Docker学习(二)——Docker容器使用
Docker容器使用 1.Docker客户端 命令docker可以查看到Docker客户端的所有命令选项. 命令docker command --help更深入的了解指定的Do ...
- js实现递归菜单无限层
/*动态加载菜单*/ function dynamicMenu(data){ if (userID != "admin"){ //1.清空所有菜单 $("#menuLis ...
- BigDecimal 计算注意事项
BigDecimal 在进行除法运算(divide)时一定要注意:如果被除数为变量,一定要指定精度 和 舍入模式,否则会报:Non-terminating decimal expansion; no ...
- Shell脚本实现自动修改IP地址
作为一名Linux SA,日常运维中很多地方都会用到脚本,而服务器的ip一般采用静态ip或者MAC绑定,当然后者比较操作起来相对繁琐,而前者我们可以设置主机名.ip信息.网关等配置.修改成特定的主机名 ...
- 将图片打印到word中
1.生成模板文件 工具类: package com.sfec.snmgr.track.utils;import com.alibam.core.wechat.util.QRCodeUtil;impor ...
- SSO(单点登录)示例
此文为转载文章,出处:https://www.cnblogs.com/jpfss/p/9273680.html SSO在我们的应用中非常常见,例如我们在OA系统登录了,我们就可以直接进入采购系统,不需 ...