Question

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:

  • Insert a character
  • Delete a character
  • Replace a character

For example, given word1 = "mart" and word2 = "karma", return 3.

Solution

DP 四要素:1. State 2. Function 3. Initialization 4. Answer

令dp[i][j]表示长度为i的word1和长度为j的word2的最小距离。假设末位分别为x和y。那么根据x和y是否相同,考虑情况如下:

1. x == y

dp[i][j] = dp[i-1][j-1]

2. x != y

a) Delete x => dp[i-1][j] + 1

b) Insert y => dp[i][j-1] + 1

c) Replace x with y => dp[i-1][j-1] + 1

dp[i][j]取a,b,c中最小值。

public class Solution {
/**
* @param word1 & word2: Two string.
* @return: The minimum number of steps.
*/
public int minDistance(String word1, String word2) {
// write your code here
int len1 = word1.length();
int len2 = word2.length();
// dp[i][j] represents min distance for word1[0, i - 1] and word2[0, j - 1]
int[][] dp = new int[len1 + 1][len2 + 1];
for (int i = 0; i <= len1; i++) {
dp[i][0] = i;
}
for (int j = 0; j <= len2; j++) {
dp[0][j] = j;
}
for (int i = 1; i <= len1; i++) {
for (int j = 1; j <= len2; j++) {
if (word1.charAt(i - 1) == word2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1];
} else {
// delete word1[i - 1]
int x = dp[i - 1][j] + 1;
// insert word2[j - 1] into word1
int y = dp[i][j - 1] + 1;
// replace word1[i - 1] with word2[j - 1]
int z = dp[i - 1][j - 1] + 1;
dp[i][j] = Math.min(x, y);
dp[i][j] = Math.min(dp[i][j], z);
}
}
}
return dp[len1][len2];
}
}

Edit Distance 解答的更多相关文章

  1. 刷题72. Edit Distance

    一.题目说明 题目72. Edit Distance,计算将word1转换为word2最少需要的操作.操作包含:插入一个字符,删除一个字符,替换一个字符.本题难度为Hard! 二.我的解答 这个题目一 ...

  2. [LeetCode] One Edit Distance 一个编辑距离

    Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...

  3. [LeetCode] Edit Distance 编辑距离

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

  4. Edit Distance

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

  5. 编辑距离——Edit Distance

    编辑距离 在计算机科学中,编辑距离是一种量化两个字符串差异程度的方法,也就是计算从一个字符串转换成另外一个字符串所需要的最少操作步骤.不同的编辑距离中定义了不同操作的集合.比较常用的莱温斯坦距离(Le ...

  6. LintCode Edit Distance

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

  7. stanford NLP学习笔记3:最小编辑距离(Minimum Edit Distance)

    I. 最小编辑距离的定义 最小编辑距离旨在定义两个字符串之间的相似度(word similarity).定义相似度可以用于拼写纠错,计算生物学上的序列比对,机器翻译,信息提取,语音识别等. 编辑距离就 ...

  8. [UCSD白板题] Compute the Edit Distance Between Two Strings

    Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...

  9. 动态规划 求解 Minimum Edit Distance

    http://blog.csdn.net/abcjennifer/article/details/7735272 自然语言处理(NLP)中,有一个基本问题就是求两个字符串的minimal Edit D ...

随机推荐

  1. 机房收费系统——在VB中将MSHFlexGrid控件中的数据导出到Excel

    机房收费系统中,好多查询的窗体都包含同一个功能:将数据库中查询到的数据显示在MSHFlexGrid控件中,然后再把MSHFlexGrid控件中的数据导出到Excel表格中. 虽然之前做过学生信息管理系 ...

  2. POJ 2976 Dropping tests 01分数规划

    给出n(n<=1000)个考试的成绩ai和满分bi,要求去掉k个考试成绩,使得剩下的∑ai/∑bi*100最大并输出. 典型的01分数规划 要使∑ai/∑bi最大,不妨设ans=∑ai/∑bi, ...

  3. 话付通SDK 聚合支付

    步骤 官网:http://www.71pay.cn/ 1.导入Jar包----将HeepayPlugin.jar,HftJuhePay.jar 包放入工程指定的libs目录. 2.配置清单文件---- ...

  4. HTML5触摸屏touch事件使用实例1

    1.源码: <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> ...

  5. PL/SQL客户端安装配置说明

    一.电脑安装了多个Oracle客户端时,需要设定pl/sql 中的home 二.配置环境变量: (打开环境变量配置界面操作:我的电脑---属性---高级---环境变量,在系统变量部分新建或编辑即可.w ...

  6. SAE下的Memcache使用方法

    SAE里面有Memcache,可以较大幅度改善数据库的鸭梨~ 之前一直想学习Memcache,却愁于不知如何下手,对这个名词完全没有概念,同时在SAE的文档里面,也很少对于Memcache的使用教程~ ...

  7. Jason 分享吴霁虹教授的产品模型

    产品的出现都是为了解决市场上存在的某一个”疼点“或一系列的”疼点“而出现. 疼点:是一个亟需待解决的问题,对应有相应的市场,会寻找相应的解决方案.比如:用户的小孩——>因为缺钱,所以担心小孩无法 ...

  8. 东软实训1 -jsp内置对象及其常用方法

    JSP 的内置对象及方法详细说明 一. request 对象 客户端的请求信息被封装在 request 对象中,通过它才能了解到客户的需 求,然后做出响应.它是 HttpServletRequest ...

  9. php wampp 访问数据库

    //获取用户信息private function UlikeGetUserInfo($Wechat,$data) { $this->logger("到达UlikeFunction.in ...

  10. 2.1 Java I/O简史

    Java 1.0 到 1.3 中的 IO 没有而 Java 1.4 中引入的 NIO 有的“改进”:非阻塞IO.缓冲区.通道层.字符集.内存数据.Perl(正则表达式之王): 下一代 I/O-NIO. ...