LintCode Edit Distance
LintCode 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:
- Insert a character
- Delete a character
- Replace a character
Example
Given word1 = "mart" and word2 = "karma", return 3
For this problem, the dynamic programming is used.
Firstly, define the state MD(i,j) stand for the int number of minimum distance of changing i-char length word to j-char length word. MD(i, j) is the result of editing word1 which has i number of chars to word2 which has j number of word.
Second, we want to see the relationship between MD(i,j) with MD(i-1, j-1) , MD(i-1, j) and MD(i, j-1).
Thirdly, initilize all the base case as MD(i, 0) = i, namely that delete all i-char-long word to zero and MD(0, i) = i, namely insert zero length word to i-char-long word.
Fourth, solution is to calculate MD(word1.length(), word2.length())
public class Solution {
/**
* @param word1 & word2: Two string.
* @return: The minimum number of steps.
*/
public int minDistance(String word1, String word2) {
int n = word1.length();
int m = word2.length();
int[][] MD = new int[n+][m+];
for (int i = ; i < n+; i++) {
MD[i][] = i;
}
for (int i = ; i < m+; i++) {
MD[][i] = i;
}
for (int i = ; i < n+; i++) {
for (int j = ; j < m+; j++) {
//word1's ith element is equals to word2's jth element
if(word1.charAt(i-) == word2.charAt(j-)) {
MD[i][j] = MD[i-][j-];
}
else {
MD[i][j] = Math.min(MD[i-][j-] + ,Math.min(MD[i][j-] + , MD[i-][j] + ));
}
}
}
return MD[n][m];
}
}
LintCode Edit Distance的更多相关文章
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- Edit Distance
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- 编辑距离——Edit Distance
编辑距离 在计算机科学中,编辑距离是一种量化两个字符串差异程度的方法,也就是计算从一个字符串转换成另外一个字符串所需要的最少操作步骤.不同的编辑距离中定义了不同操作的集合.比较常用的莱温斯坦距离(Le ...
- stanford NLP学习笔记3:最小编辑距离(Minimum Edit Distance)
I. 最小编辑距离的定义 最小编辑距离旨在定义两个字符串之间的相似度(word similarity).定义相似度可以用于拼写纠错,计算生物学上的序列比对,机器翻译,信息提取,语音识别等. 编辑距离就 ...
- [UCSD白板题] Compute the Edit Distance Between Two Strings
Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...
- 动态规划 求解 Minimum Edit Distance
http://blog.csdn.net/abcjennifer/article/details/7735272 自然语言处理(NLP)中,有一个基本问题就是求两个字符串的minimal Edit D ...
- One Edit Distance
Given two strings S and T, determine if they are both one edit distance apart. 分析:https://segmentfau ...
- 【leetcode】Edit Distance
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
随机推荐
- 【转】javascript打印设置
页面中的代码:<OBJECT id="WebBrowser1" height="0" width="0" classid ...
- 在数据库中varchar与char的区别
char是固定长度类型 varchar是可变的长度类型 char和varchar类型相似,但是它们的存储和检索方式不同.在MySQL5.0.3,它们在最大长度和是否保留尾部空格也不相同. char和v ...
- The trip(Uva 11100)
题目大意: 给出n个数,要求将其分成最少的递增序列,保证序列最少的同时要使得序列长度的最大值最小. n<=10000 题解: 1.我是直接看着<训练指南>里的中文题面的,lrj似乎 ...
- 计算机网络(12)-----HTTP协议详解
HTTP协议详解 http请求 http请求由三部分组成,分别是:请求行.消息报头.请求正文 (1)请求行 请求行以一个方法符号开头,以空格分开,后面跟着请求的URI和协议的版本,格式如下:Metho ...
- asp.net错误页和asp.net mvc错误页设置
asp.net错误页 在日常项目开发过程中,我们需要给网站设置错误页和记录错误日志. 首先,在项目中添加全局应用程序类 在Global.asax中 protected void Application ...
- JSHelper时间格式化
Helper.prototype.FormatDate = function (format) { var _now = new Date(); var o = { "M+": _ ...
- Go语言http包Form解析之坑
最近正在用Go语言做一个项目,今天在用http包读取客户端发过来的POST数据时遇到了一点小麻烦,就下面这段代码,死活读不到数据: { var body []byte nRead, err := r. ...
- hashmap 读取
hashTable hashSet 都差不多 以hashmap为例,底层是一个散列表 数组,然后数组存出一个entry对象,对象中有两个泛型属性,一个可以指向自身类型的引用,这样就可以在每一个数组的位 ...
- oracle应用-表空间.用户名.权限
oracle 数据库应用 虽然代码少但是很实用! --01.表空间create tablespace tp_hrdatafile 'E:\tp_hr01.dbf' size 10M, 'E:\tp_t ...
- 【转】slice,substr和substring的区别
首先,他们都接收两个参数,slice和substring接收的是起始位置和结束位置(不包括结束位置),而substr接收的则是起始位置和所要返回的字符串长度.直接看下面例子: var test = ' ...