ref: https://discuss.leetcode.com/topic/3136/my-o-mn-time-and-o-n-space-solution-using-dp-with-explanation/2

如果用dp[i][j]表示word1里[0,i]长的子串和word2[0,j]的最小编辑距离,那么状态转移方程应该这样构成:

dp[i][j] = dp[i-1][j-1], if word1[i] == word2[j]dp[i][j] = max{dp[i-1][j], dp[i][j-1], dp[i-1][j-1]} + 1, otherwise

如果word1[i] == word2[j]的时候不需要解释,

如果不相等的时候可能有三种操作,使两个字符串对齐:

  1. 替换字母:把word1[i] 替换成word2[j],那么是dp[i-1][j-1]+1

  2. 在Word1[i]后面强行插入一个word2[j]. 所以可能是dp[i][j-1]

  3. 直接把word1[i]删了,dp[i-1][j]

所以用二维矩阵表示很容易:

 1 public int minDistance(String word1, String word2) {
 2     int len1 = word1.length();
 3     int len2 = word2.length();
 4
 5     //distance[i][j] is the distance converse word1(1~ith) to word2(1~jth)
 6     int[][] distance = new int[len1 + 1][len2 + 1];
 7     for (int j = 0; j <= len2; j++)
 8         {distance[0][j] = j;} //delete all characters in word2
 9     for (int i = 0; i <= len1; i++)
10         {distance[i][0] = i;}
11
12     for (int i = 1; i <= len1; i++) {
13         for (int j = 1; j <= len2; j++) {
14             if (word1.charAt(i - 1) == word2.charAt(j - 1)) { //ith & jth
15                 distance[i][j] = distance[i - 1][j - 1];
16             } else {
17                 distance[i][j] = Math.min(Math.min(distance[i][j - 1], distance[i - 1][j]), distance[i - 1][j - 1]) + 1;
18             }
19         }
20     }
21     return distance[len1][len2];
22 }

但是,可以像uniquePath一样把二维压成一维

pre表示上同一行前一列的值,f[j-1]表示f[i-1][j-1](因为pre没有更新进去),f[j]表示同一列上一行

 1     public int minDistance(String word1, String word2) {
 2         if(word1 == null || word2 == null) {
 3             return -1;
 4         }
 5         int len1 = word1.length();
 6         int len2 = word2.length();
 7         int[] distance = new int[len2 + 1];
 8         for(int i = 0; i <= len2; i++) {
 9             distance[i] = i;
10         }
11         for(int i = 1; i <= len1; i++) {
12             int pre = i;
13             for(int j = 1; j <= len2; j++) {
14                 int cur = 0;
15                 if(word1.charAt(i - 1) == word2.charAt(j - 1)) {
16                     cur = distance[j - 1];
17                 } else {
18                     cur = Math.min(pre, Math.min(distance[j], distance[j - 1])) + 1;
19                 }
20                 distance[j - 1] = pre;
21                 pre = cur;
22             }
23             distance[len2] = pre;
24         }
25         return distance[len2];
26     }

转载于:https://www.cnblogs.com/warmland/p/5971909.html

72. Edit Distance相关推荐

  1. 【重点!记忆化递归+DP】LeetCode 72. Edit Distance

    LeetCode 72. Edit Distance 参考链接:https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-72-edit ...

  2. LeetCode(72)Edit Distance

    题目 Given two words word1 and word2, find the minimum number of steps required to convert word1 to wo ...

  3. [leetcode] 72.Edit Distance 编辑距离-史前最简明清晰的解答

    题目: 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 输入: ...

  4. LeetCode 72. Edit Distance--动态规划--Levenshtein Distance Algorithm--Java,Python解法

    此题链接:Edit Distance - LeetCode LeetCode 动态规划(Dynamic programming)系列题目:LeetCode 动态规划(Dynamic programmi ...

  5. 字符串编辑距离(Edit Distance)

    一.问题描述 定义 字符串编辑距离(Edit Distance),是俄罗斯科学家 Vladimir Levenshtein 在 1965 年提出的概念,又称 Levenshtein 距离,是指两个字符 ...

  6. leetcode Edit Distance

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051082.html 题目链接:leetcode Edit Distance 最短编辑距离, ...

  7. 详解编辑距离(Edit Distance)及其代码实现

    概述 编辑距离(Minimum Edit Distance,MED),由俄罗斯科学家 Vladimir Levenshtein 在1965年提出,也因此而得名 Levenshtein Distance ...

  8. Edit Distance编辑距离(NM tag)- sam/bam格式解读进阶

    sam格式很精炼,几乎包含了比对的所有信息,我们平常用到的信息很少,但特殊情况下,我们会用到一些较为生僻的信息,关于这些信息sam官方文档的介绍比较精简,直接看估计很难看懂. 今天要介绍的是如何通过b ...

  9. Edit Distance Python源码及支持包的实现

    Edit Distance Python源码及支持包的实现 编辑距离 编辑距离 又称Levenshtein距离(莱文斯坦距离也叫做Edit Distance)指两个字串之间,由一个转成另一个所需的最少 ...

最新文章

  1. 梯度下降原理及线性回归代码实现(python/java/c++)
  2. 机器学习中的标签泄漏介绍及其如何影响模型性能
  3. vba给服务器发送消息,使用VBA实现发邮件功能
  4. Activity中加载器的总结
  5. ITK:KMeans聚类
  6. centos下配置vsftpd
  7. 前端大框架知识归纳与总结
  8. c++实现二叉树操作
  9. Java并发编程实战————可重入内置锁
  10. 判断textarea是否超过行数限制
  11. 7系统启动到一半停止_扛不住了!可口可乐巨震:500个品牌砍一半,裁员4000人.........
  12. 剑指offer之数组中的数据查找
  13. 创建和销毁对象(1)
  14. 闪迪u盘量产工具万能版_我身边的“闪迪色”闪迪彩色手机U盘系列| 大家测573...
  15. Matlab之数据的输入与输出
  16. w10计算机字体怎么设置在哪里设置,如何设置修改win10系统电脑的显示字体
  17. 产品设计体会(4013)产品路标规划
  18. win10系统开启扫描仪服务器,win10怎么安装扫描仪 win10扫描仪怎么扫描
  19. SAR图像超分辨技术
  20. Combined Cycle Power Plant Data Set(初学练手:详解)

热门文章

  1. 自定义类型详解:结构体(内存对齐、位段) + 枚举 + 联合
  2. html两行中间间距怎么去,css行之间的间距怎么调?
  3. 出场顺序很重要下一句_人生如戏,出场顺序很重要:再见,不负遇见
  4. 广域网访问局域网路由器设置_交换机路由器如何连接 交换机路由器连接方法【详解】...
  5. 图解PCB板元器件焊接流程
  6. NYOJ-水池数目(dfs)
  7. DCMTK DCMSCU例子
  8. EditorGridPanel 中使用checkbox列,并包含afterEdit事件
  9. Django-ModelFrom中修改save后的字段值
  10. 实时计算轻松上手,阿里云DataWorks Stream Studio正式发布