题目:

Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2.

You have the following 3 operations permitted on a word:

  1. Insert a character
  2. Delete a character
  3. Replace a character

Example 1:

Input: word1 = "horse", word2 = "ros"
Output: 3
Explanation:
horse -> rorse (replace 'h' with 'r')
rorse -> rose (remove 'r')
rose -> ros (remove 'e')

Example 2:

Input: word1 = "intention", word2 = "execution"
Output: 5
Explanation:
intention -> inention (remove 't')
inention -> enention (replace 'i' with 'e')
enention -> exention (replace 'n' with 'x')
exention -> exection (replace 'n' with 'c')
exection -> execution (insert 'u')

分析:

给定两个单词,求word1转换到word2需要的最少步骤,转换的操作有三种,分别是插入一个字符,删除一个字符,替换一个字符。

d(word1, word2)用来求两个单词转换的需要的最小步数,那么如果当两个单词的最后一个字符是相同的,则d(word1, word2) = d(word1', word2')其word1'和word2'是分别去掉最后一个字符的单词。

如果最后两个字符不相同时,我们就需要操作来进行转换,一种是在word1后增加一个字符,是其最后一个字符和word2的最后一个字符相同,一种是删去word1的最后一个字符,一种是将word1的最后一个字符转换成word2的最后一个字符,那么此时最小的步数就是前三个操作的最小值加上1.

可能有同学会问为什么不在word2上进行操作,实际上操作转换这一步是有5个子问题的,但实际上在word1后增加一个字符和word2最后字符相同,相当于在word2后删除字符;删去word1的字符相当于在word2后增加一个字符和word1最后字符相同;而转换操作明显是一样的,所以就合并成为了三个子问题。

当递归执行到其中一个串为空串时,则加上另一个串的长度即可,相当于删去所有的字符。

程序:

C++

class Solution {
public:
int minDistance(string word1, string word2) {
int l1 = word1.length();
int l2 = word2.length();
dp = vector<vector<int>>(l1+1, vector<int>(l2+1, -1));
return minDistance(word1, word2, l1, l2);
}
private:
vector<vector<int>> dp;
int minDistance(string& word1, string& word2, int l1, int l2){
if(l1 == 0)
return l2;
if(l2 == 0)
return l1;
if(dp[l1][l2] >= 0)
return dp[l1][l2];
int res = 0;
if(word1[l1-1] == word2[l2-1]){
res = minDistance(word1, word2, l1-1, l2-1);
dp[l1][l2] = res;
return res;
}
res = min(minDistance(word1, word2, l1-1, l2),
min(minDistance(word1, word2, l1, l2-1),
minDistance(word1, word2, l1-1, l2-1))) + 1;
dp[l1][l2] = res;
return res;
}
};

Java

class Solution {
public int minDistance(String word1, String word2) {
int l1 = word1.length();
int l2 = word2.length();
dp = new int[l1+1][l2+1];
for(int i = 0; i < dp.length; ++i){
for(int j = 0; j < dp[i].length; ++j){
dp[i][j] = -1;
}
}
return minDistance(word1, word2, l1, l2);
}
private int minDistance(String word1, String word2, int l1, int l2){
if(l1 == 0) return l2;
if(l2 == 0) return l1;
if(dp[l1][l2] >= 0)
return dp[l1][l2];
int res = 0;
if(word1.charAt(l1-1) == word2.charAt(l2-1)){
res = minDistance(word1, word2, l1-1, l2-1);
}else{
res = Math.min(minDistance(word1, word2, l1-1, l2),
Math.min(minDistance(word1, word2, l1, l2-1),
minDistance(word1, word2, l1-1, l2-1))) + 1;
}
dp[l1][l2] = res;
return res;
}
private int[][] dp;
}

LeetCode 72. Edit Distance 编辑距离 (C++/Java)的更多相关文章

  1. [LeetCode] 72. Edit Distance 编辑距离

    Given two words word1 and word2, find the minimum number of operations required to convert word1 to  ...

  2. leetCode 72.Edit Distance (编辑距离) 解题思路和方法

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

  3. [LeetCode] 72. Edit Distance(最短编辑距离)

    传送门 Description Given two words word1 and word2, find the minimum number of steps required to conver ...

  4. 【LeetCode】72. Edit Distance 编辑距离(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 记忆化搜索 动态规划 日期 题目地址:http ...

  5. LeetCode - 72. Edit Distance

    最小编辑距离,动态规划经典题. Given two words word1 and word2, find the minimum number of steps required to conver ...

  6. [leetcode]72. Edit Distance 最少编辑步数

    Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...

  7. 72. Edit Distance(编辑距离 动态规划)

    Given two words word1 and word2, find the minimum number of operations required to convert word1 to  ...

  8. 第十八周 Leetcode 72. Edit Distance(HARD) O(N^2)DP

    Leetcode72 看起来比较棘手的一道题(列DP方程还是要大胆猜想..) DP方程该怎么列呢? dp[i][j]表示字符串a[0....i-1]转化为b[0....j-1]的最少距离 转移方程分三 ...

  9. [leetcode] 72. Edit Distance (hard)

    原题 dp 利用二维数组dp[i][j]存储状态: 从字符串A的0~i位子字符串 到 字符串B的0~j位子字符串,最少需要几步.(每一次删增改都算1步) 所以可得边界状态dp[i][0]=i,dp[0 ...

  10. 【Leetcode】72 Edit Distance

    72. Edit Distance Given two words word1 and word2, find the minimum number of steps required to conv ...

随机推荐

  1. 基于 EventBridge 构建数据库应用集成

    ​简介:本文重点介绍 EventBridge 的新特性:数据库 Sink 事件目标. 作者:赵海 引言 事件总线 EventBridge 是阿里云提供的一款无服务器事件总线服务,支持将阿里云服务.自定 ...

  2. OceanBase首次阐述战略:继续坚持自研开放之路 开源300万行核心代码

    简介: 在数据库OceanBase3.0峰会上,蚂蚁集团自主研发的分布式数据库OceanBase首次从技术.商业和生态三个维度对未来发展战略进行了系统性阐述.同时,OceanBase宣布正式开源,并成 ...

  3. vue通过input选取apk文件上传,显示进度条

    <template> <div class=""> <form action="" method="post" ...

  4. Competition Set - 2023杭电多校

    关于代码太多导致太卡然后发现刚好可以放下HDU多校这回事. 那就稍微加点经历吧.team316. 第十场 今天发挥还行. 上来开03,一发过.然后做09,感觉就不难,过之.发现11是数学题,干,想了2 ...

  5. 【VMware vCenter】连接和使用vCenter Server嵌入式vPostgres数据库。

    vCenter Server 早期支持内嵌(embedded)和外部(external)数据库,内嵌数据库就是vPostgres,基于VMware Postgres数据库(PostgreSQL数据库) ...

  6. 超轻量级的c#版基于文件的日志记录工具,可定制输出格式,可指定日志文件

    这是我自己个人编写的日志记录,主要使用在只需要记录日志,偶尔到文件中查看一下日志记录的情况.我自己写的一些服务之类的是使用了这个的,代码很少,使用很简单. 第一步 搜索和安装我的Nuget包 搜索和安 ...

  7. WEB服务与NGINX(24)- LNMP架构部署wordpress

    目录 1. LNMP架构项目实战 1.1 LNMP架构介绍 1.2 LNMP架构部署wordpress 1.2.1 LNMP环境介绍 1.2.2 二进制部署mariadb 1.2.3 部署php-fp ...

  8. 原生微信小程序

    new Date 跨平台兼容性问题 在 Andriod 使用 new Date("2018-05-30 00:00:00")木有问题,但是在ios 下面识别不出来.因为 IOS 下 ...

  9. 厉害了!12秒将百万数据通过EasyExcel导入MySQL数据库中

    一.写在开头 我们在上一篇文章中提到了通过EasyExcel处理Mysql百万数据的导入功能(一键看原文),当时我们经过测试数据的反复测验,100万条放在excel中的数据,4个字段的情况下,导入数据 ...

  10. 腾讯、阿里、B站最新面经汇总,有的妥妥的凉经

    除了BAT(没错我说的B是B站的B),还有网易.希音科技.美柚等中小厂的最新面经. 这次投稿的同学行文幽默风趣,处处透漏着不成功便搞笑的骚气. 祝他早日上岸,也欢迎大家在评论区讨论这些面试题,有哪些面 ...