题目:

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. ImageJ软件使用教程(三):目标计数

    目录 多点工具法 阀值分割法 二值化 填充分割 自动计数 显示结果 总结 参考资料 本文以钢筋计数为例,讲解一下如何使用ImageJ软件进行计数,这里只介绍两种方法: 多点工具法 阀值分割法 钢筋计数 ...

  2. 如何落地云原生DevOps?

    简介: 什么是云原生DevOps?在阿里内部有怎样的实践?企业又该如何落地?阿里云云效专家团队提出了下一代精益产品开发方法体系--ALPD,提供了系统的云原生DevOps落地的方法支撑,帮助企业渐进式 ...

  3. 阿里云PolarDB开源数据库社区与 Tapdata 联合共建开放数据技术生态

    ​简介:近日,阿里云PolarDB开源数据库社区宣布将与 Tapdata 联合共建开放数据技术生态. 近日,阿里云PolarDB开源数据库社区宣布将与 Tapdata 联合共建开放数据技术生态.在此之 ...

  4. SaaS服务的私有化部署,这样做最高效|云效工程师指北

    ​简介:为了能够有效且高效地同时管理SaaS版本和私有化版本的发布过程,云效团队也结合云原生的基础设施和标准化工具(比如helm)进行了一系列的探索和实践,并将其中一些通能的能力进行了产品化.本文从问 ...

  5. 实时 OLAP, 从 0 到 1

    简介: BTC.com 团队在实时 OLAP 方面的技术演进过程及生产优化实践. 作者|高正炎 本文主要介绍 BTC.com 团队在实时 OLAP 方面的技术演进过程及生产优化实践,内容如下: 业务背 ...

  6. [TP5] 动态绑定指定默认模块, 解决: 控制器不存在:app\index\controller\Api

    当在 TP5 入口中简单使用 define('BIND_MODULE','index') 绑定默认模块后,访问 api 模块会提示: 控制器不存在:app\index\controller\Api 这 ...

  7. 为什么我反对过度使用TypeScript?

    前言 在2024年, TypeScript肯定算不上什么新鲜的技术. 但是经过长时间的使用, 我认为可以使用, 但是要适度. 类型跟不上业务的变化 我们知道TypeScript的类型定义是业务的体现. ...

  8. Lua 学习笔记(自用)

    Lua 学习笔记 1 语言基础 运行方式类似Python,可以直接在交互行运行,也可以通过解释器运行某个脚本.也可以在交互行运行某个lua脚本 dofile("hello.lua" ...

  9. Data Lake_理解数据湖

    Pentaho首席技术官James Dixon创造了"数据湖"一词.它把数据集市描述成一瓶水(清洗过的,包装过的和结构化易于使用的).而数据湖更像是在自然状态下的水,数据流从源系统 ...

  10. 在英特尔至强 CPU 上使用 🤗 Optimum Intel 实现超快 SetFit 推理

    在缺少标注数据场景,SetFit 是解决的建模问题的一个有前途的解决方案,其由 Hugging Face 与 Intel 实验室 以及 UKP Lab 合作共同开发.作为一个高效的框架,SetFit ...