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. Android(java)学习笔记252:ContentProvider使用之内容观察者01

    1. 内容观察者 不属于四大组件,只是内容提供者ContentProvider对应的小功能. 如果发现数据库内容变化了,就会立刻观察到. 下面是逻辑图:       当A应用中银行内部的数据发生变化的 ...

  2. 关于centos6.5系统安装FTP服务和配置的方法

    一般在配置服务器的时候,涉及到代码上传,通常都要用到FTP方式. 1.先查看系统是否安装vsftpd: rpm -qa | grep vsftpd 如果出现vsftpd-2.2.2-14......字 ...

  3. C#链接远程SQL 服务器方法

     C#链接远程SQL 服务器方法第一步:申请花生壳内网版,要求交1块钱给花生壳服务器做验证.第二步:把你自己主机本地连接那里的内网地址不要自动获取,写成192.168.0.105,子网掩码255.25 ...

  4. Android开发手记(11) 滑动条SeekBar

    安卓滑动条的操作特别简单,通过getProgress()可以获得SeekBar的位置,通过setProgress(int progress)可以设置SeekBar的位置.要想动态获取用户对SeekBa ...

  5. Arcgis Desktop 9.3 安装

    以下用到的 Crack在我的网盘中有: ref: http://pan.baidu.com/s/1pJJlVBl 密码: p4gk 一,安装 Desktop(依次按照如图安装): 二,配置 1,以上步 ...

  6. InstallShield 覆盖安装

    “吾乐吧软件站”提供了很全面详细的InstallShield制作安装包教程(http://www.wuleba.com/23892.html),但是按上面的方法再次制作的升级安装包,安装后会在系统中同 ...

  7. Mysql 视图笔记

    1.       视图的定义 视图就是从一个或多个表中,导出来的表,是一个虚拟存在的表.视图就像一个窗口(数据展示的窗口),通过这个窗口,可以看到系统专门提供的数据(也可以查看到数据表的全部数据),使 ...

  8. 快速幂:quickpow

    众所周知,快速幂是优化对数的次方运算的最普遍手段.在学习快速幂的思想时,其分治思想容易让大家用简单的递归实现. 但其实,除了递归之外,更好的方法会是简单的 WHILE循环.下面贴代码: #includ ...

  9. CListBox控件基本功能

    创建CListBox对象 CListBox m_ListBox;关联控件 ,同时注意行数从  0  开始计算 1.向控件中添加内容 int AddString(LPCTSTR lpszItem ); ...

  10. JSP中取COOKIE中指定值得方法【转载】

    Cookie cookies[]=request.getCookies(); //读出用户硬盘上的Cookie,并将所有的Cookie放到一个cookie对象数组里面 Cookie sCookie=n ...