Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)

You have the following 3 operations permitted on a word:

a) Insert a character

b) Delete a character

c) Replace a character

别人的思路:

自然语言处理(NLP)中。有一个基本问题就是求两个字符串的minimal Edit Distance, 也称Levenshtein distance。受到一篇Edit Distance介绍文章的启示。本文用动态规划求取了两个字符串之间的minimal Edit Distance. 动态规划方程将在下文进行解说。

1. what is minimal edit distance?

简单地说。就是仅通过插入(insert)、删除(delete)和替换(substitute)个操作将一个字符串s1变换到还有一个字符串s2的最少步骤数。熟悉算法的同学非常easy知道这是个动态规划问题。
事实上一个替换操作能够相当于一个delete+一个insert,所以我们将权值定义例如以下:
I (insert):1
D (delete):1
S (substitute):2 2. example:
intention->execution
Minimal edit distance:
delete i ; n->e ; t->x ; insert c ; n->u 求和得cost=8 3.calculate minimal edit distance dynamically
思路见凝视,这里D[i,j]就是取s1前i个character和s2前j个character所得minimal edit distance
三个操作动态进行更新:
D(i,j)=min { D(i-1, j) +1, D(i, j-1) +1 , D(i-1, j-1) + s1[i]==s2[j] ? 0 : 2}。中的三项分别相应D,I,S。(详见我同学的博客)

由于本题的替换操作权重相同为1。故字符不相等+1就可以。

代码例如以下:

public class Solution {
public int minDistance(String word1, String word2) {
//边界条件
if(word1.length() == 0)
return word2.length();
if(word2.length() == 0)
return word1.length();
/*
* 本题用动态规划的解法
* f[i][j]表示word1的前i个单词到word2前j个单词的最短距离
* 状态转移方程:f[i][j] =
*/ int[][] f = new int[word1.length()][word2.length()];
boolean isEquals = false;//是否已经有相等
for(int i = 0 ; i < word2.length(); i++){
//假设相等,则距离不添加
if(word1.charAt(0) == word2.charAt(i) && !isEquals){
f[0][i] = i > 0 ? f[0][i-1]:0;//不能从0開始
isEquals = true;
}else{
f[0][i] = i > 0 ? f[0][i-1]+1:1;
}
}
isEquals = false;//是否已经有相等
for(int i = 1 ; i < word1.length(); i++){
//假设相等,则距离不添加
if(word1.charAt(i) == word2.charAt(0) && !isEquals){
f[i][0] = f[i-1][0];//不能从0開始
isEquals = true;
}else{
f[i][0] = f[i-1][0]+1;
}
} for(int i = 1; i < word1.length();i++){
for(int j = 1; j < word2.length(); j++){
if(word1.charAt(i) == word2.charAt(j)){
f[i][j] = f[i-1][j-1];//相等的话直接相等
}else{
f[i][j] = f[i-1][j-1]+1;
}
//然后与从f[i-1][j]+1。f[i][j-1]+1比較,取最小值
f[i][j] = Math.min(f[i][j],Math.min(f[i-1][j]+1,f[i][j-1]+1));
}
}
return f[word1.length()-1][word2.length()-1];
}
}

leetCode 72.Edit Distance (编辑距离) 解题思路和方法的更多相关文章

  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(最短编辑距离)

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

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

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

  4. LeetCode - 72. Edit Distance

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

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

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

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

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

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

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

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

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

  9. leetCode 48.Rotate Image (旋转图像) 解题思路和方法

    Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...

随机推荐

  1. C#趣味程序---三色球问题

    问题:若一个口袋中放有12个球,3红3白和6黑,问从袋中随意取8个球,有多少种不同的颜色搭配? using System; namespace ConsoleApplication1 { class ...

  2. Linux 查找一个函数在哪调用

    grep "function has" -R ThinkPHP

  3. Ubuntu(64位)编译Android源码常见错误解决办法

    2013年07月10日 14:47:27 阅读数:1239 错误: /usr/include/gnu/stubs.h:7:27: error: gnu/stubs-32.h: No such file ...

  4. JAVA设计模式之【原型模式】

    1.案例一 学生复制 package Prototype; /** * Created by Jim on 2016/10/1. */ public class Student implements ...

  5. iOS开发中权限再度梳理

    前言 上篇文章iOS开发中的这些权限,你搞懂了吗?介绍了一些常用权限的获取和请求方法,知道这些方法的使用基本上可以搞定大部分应用的权限访问的需求.但是,这些方法并不全面,不能涵盖住所有权限访问的方法. ...

  6. AngularJS 下拉列表demo

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script sr ...

  7. Weex学习与实践(一):Weex,你需要知道的事

    Weex学习与实践(一):Weex,你需要知道的事 http://coderyi.com/posts/weex1/ 1.命令行工具:weex-toolkit  https://github.com/w ...

  8. JavaScript中数组的迭代方法:forEach、map、filter、reduce、every、some、for in、for of

    JavaScript中有非常多数组迭代方法,这里基本上吧所有的都介绍全了,我项目中比较喜欢的是forEach. 7.for in (for-in循环实际是为循环对象而设计的,for in也可以循环数组 ...

  9. sqlserver数据文件位置如何迁移

    亲测有效的一种方式: 1.对应的数据库脱机 2.迁移物理文件 3.删除原有实例 4.附加

  10. SQL Server数据库性能优化

      开篇:    最近遇到了很多性能问题,一直没来的及总结,今天正好周末抽时间总结下: 对于稍微大点的公司或者说用户多一些的公司,说白了就是数据量较大的公司,在查询数据时往往会遇到很多瓶颈.这时就需要 ...