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 ...
随机推荐
- idea 中利用maven创建java web 项目
转自:http://www.linuxidc.com/Linux/2014-04/99687.htm 本文主要使用图解介绍了使用IntelliJ IDEA 12创建Maven管理的Java Web项目 ...
- robots笔记以免忘记
html头部标签写法: <meta name="robots" content="index,follow" /> content中的值决定允许抓取 ...
- C++ 11 lambda
转载:http://www.cnblogs.com/kedebug/p/3224561.html lambda 表达式的简单语法如下:[capture] (parameters) -> retu ...
- VMware下利用ubuntu13.04建立嵌入式开发环境之三
系统环境建立完成后就要安装和配置嵌入式开始需要的工具和服务. 一般我们在交叉编译是需要的服务有:smb.tftp.telnet.nfs.ssh和x11等.下面一步步,介绍如何安装这些服务. 一.smb ...
- python 异常处理学习笔记
搬运至慕课网,精华截图,视频链接在这 : http://www.imooc.com/learn/457 1. 异常检查目的 2. python 可能出现的异常 3. 异常的处理过程 try - ex ...
- 一个比较综合的项目--》>图片缓存,下拉刷新等
在办公室电脑(E:\workspace\23\Collections)
- hosts持续更新
Google hosts网址: https://laod.cn/hosts/2016-google-hosts.html
- php 错误处理函数
eval() 把子符串当做php 代码执行 // 回调函数function a($b, $c) { echo $b; echo $c; } call_user_func_array('a', ar ...
- GeoHash原理解析
GeoHash 核心原理解析 引子 一提到索引,大家脑子里马上浮现出B树索引,因为大量的数据库(如MySQL.oracle.PostgreSQL等)都在使用B树.B树索引本质上是对索引字段 ...
- MFC 网络编程中::connect返回-1问题
在MFC编写网络时遇到了::connect总是返回-1,但是与服务器可以进行接收和发送消息的操作. 原因是在进行连接的时候我没有进行初始化:::WSAStartup(w, &data);//动 ...