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 ...
随机推荐
- oracle 递归应用(挺复杂的)
最近做数据过滤觉得很有必要记录下整个过程,说不定下次就不知道了. 废话不多说开始: 表结构: 企业表(自关联,采用树的形式记录分子公司) 区域表(自关联,采用树的形式记录省/市/县/乡,数据量大) 公 ...
- Hibernate实体关系映射(OneToMany、ManyToOne双边)——完整实例
双边关系是最常见的配置.在多方有一方的引用,一方也有多方的引用.双边关系能够很方便地查询数据.看一个班级与学生的双边关系. 班级(Clazz类)与学生(Student类):一对多关系.班级中有学生属性 ...
- ittun配置和使用教程
有好久没写博了.时间过得真快,一下子一年又过去了. 不墨迹这么多了. 作为一个前端,页面写好了少不了做测试以及给其他同事看看效果,然而公司没有测试服务器,真是悲催哦. 一直都是用ip地址看页面的.但是 ...
- Android按钮的各个样式设置
安卓开发学习之014 Button应用详解(样式.背景.按钮单击.长按.双击.多击事件) 一.Button简介 按钮也是继承自TextView 二.XML定义方法 <Button android ...
- JS日期类型处理
Date 操作日期和时间的对象 Date.getDate( ) 返回一个月中的某一天 Date.getDay( ) 返回一周中的某一天 Date.getFullYear( ) 返回Date对象的年份字 ...
- RHEL6.4编译安装企业级LAMMP平台
一.LAMMP简介 二.使用软件及服务器架构说明 三.配置及安装过程 1.安装arp与httpd 2.安装mysql 3.安装php(php-fpm) 4.安装Xcache ...
- Ajax读取txt并对txt内容进行分页显示
function TransferString(content) { var string = content; try{ string=string.replace(/\r\n/g,"&l ...
- Object类clone方法的自我理解
网上搜帖: clone()是java.lang.Object类的protected方法,实现clone方法: 1)类自身需要实现Cloneable接口 2)需重写clone()方法,最好设置修饰符mo ...
- [转载] javascript实现深度克隆
js一般有两种不同数据类型的值: 基本类型(包括undefined,Null,boolean,String,Number),按值传递: 引用类型(包括数组,对象),按址传递,引用类型在值传递的时候是内 ...
- The property on could not be set to a 'Int16' value.You must set this property to a non-null value of type ‘Int32’.”
在vs2010 EF4中查询数据总是报错: The property on “XX” could not be set to a 'Int16' value. You must set this p ...