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 ...
随机推荐
- 极光推送Jpush(v3)服务端PHP版本集成(V3版本只调用推送API)
因为版本升级,极光推送的API也有了V3,功能也更丰富了,但是对于我们有的用户来说,我们还是只需要调用推送的API就够了. 下载了一份PHP服务端的SDK(下载地址:http://docs.jpush ...
- Crowd 2.7汉化中文包(原创首发)
介绍:Crowd是用来集成Atlassian各类产品用户集成系统,如Jira,Confluence等的集中用户管理平台.可对组.成员关系.用户.目录.应用程序及权限进行综合管理,并可实现其他程序的单点 ...
- HalconMFC(一)之多版本配置
今天比较匆忙,还得写周六日考试扯P的PPT,就先这样开个头吧.我的电脑是win7,32位的系统,我用Halcon10.0.但是很多小伙伴的都是64位系统的,所以我用小伙伴的64位系统试过很多次用VC配 ...
- CALayer anchorPoint 锚点始终为(0,0)
objc.io 学习 摘自原处修改 对层的属性详细了解可见这里 @interface ClockFace : CAShapeLayer@property (nonatomic, strong) NSD ...
- Qt之C语言有符号数与无符号数运算
以32位的stm32f4为例: 1. uint32_t t_int_k = 239773, t_int_km1 = 4294859707; 则t_int_k - t_int_km1 > 0; ...
- SharePoint Site "Language Settings"功能与CSOM的对应
博客地址:http://blog.csdn.net/FoxDave SharePoint网站中的语言设置:"Language Settings",可以用CSOM通过Site的一些 ...
- Number of 1 Bits(Difficulty: Easy)
题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...
- dedecms购物车商品添加删除数量改变方式变成ajax
简单的做了一下修改,模板用的它默认的模板,感觉之前全是表单提交很不爽用的 修改的相关文件:/plus/posttocar.php, /plus/car.php,/templets/plus/car.h ...
- 初探NIOS II之hello_world
平台背景: 操作系统:win7 64bit 开发板:DE2-115 Quartus ii:15.0及配套的NIOS ii开发平台 一.硬件系统的建立 1.在Quartus里新建工程,这是很基本的就不 ...
- GitHub 上一份很受欢迎的前端代码优化指南
http://segmentfault.com/a/1190000002587334?utm_source=weekly&utm_medium=email&utm_campaign=e ...