Edit Distance 解答
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 解答的更多相关文章
- 刷题72. Edit Distance
一.题目说明 题目72. Edit Distance,计算将word1转换为word2最少需要的操作.操作包含:插入一个字符,删除一个字符,替换一个字符.本题难度为Hard! 二.我的解答 这个题目一 ...
- [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 ...
- LintCode Edit Distance
LintCode Edit Distance Given two words word1 and word2, find the minimum number of steps required to ...
- 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 ...
随机推荐
- [Redux] Reducer Composition with combineReducers()
Previous, we do composition with objects: const todoApp = (state = {}, action) => { return { todo ...
- 转 [教程] Unity3D中角色的动画脚本的编写(二)
在上一篇,我们介绍了有关Animation这个类中的部分方法,我后来想了想,这么介绍也不是个办法(其实有些方法我自己也没用过),该介绍点实际的东西了,毕竟我们是要做东西出来的.那好 ...
- http to https automatic--weblogic/jboss/tomcat--reference
weblogic reference from:http://middlewaremagic.com/weblogic/?p=2019 Many times we want to secure our ...
- [转] What is Ec/Io (and Eb/No)?
PS:http://www.telecomhall.com/what-is-ecio-and-ebno.aspx If someone asks you "Which Signal Leve ...
- javascript:void(0)知多少
在做页面时,如果想做一个链接点击后不做任何事情,或者响应点击而完成其他事情,可以设置其属性 href = "#",但是,这样会有一个问题,就是当页面有滚动条时,点击后会返回到页面顶 ...
- svg学习笔记
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- C++关联容器<map>简单总结
C++关联容器<map>简单总结 map提供大小可变的关联容器,基于关联键值高效检索元素值.当你处理键值对的数据是,都可以考虑使用map关联容器. 特点: 大小可变的关联容器,基于关联键值 ...
- sql 判断表、列、视图等是否存在
1 判断数据库是否存在 if exists (select * from sys.databases where name = '数据库名') drop database [数据库名] 2 判 ...
- Code First研究学习1_Reverse Enginner Code First
最近因为公司需要,自己开始研究Code First,之前还是听说过这个,也知道是代码优先的意思!至于具体怎么的代码优先,我的理解如下! 在听说code first的时候,心里也就觉得怪了,是怎么将M ...
- JavaScript--基本包装类型+Math对象
1. 基本包装类型 1)为了便于操作基本类型值,ECMAScript提供了3个特殊的引用类Boolean, Number, String 每当读取一个基本类型值的时候,后台就会创建一个对应 ...