Hard-题目13：72. 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:

a) Insert a character
b) Delete a character
c) Replace a character

————赵本山小品台词迷之乱入。。。

（1） 把word1的前i-1个字符变成word2的前j-1个字符，再修改word1[i]为word2[j]；
（2） 把word1的前i个字符变成word2的前j-1个字符，再加上word2[j]；
（3） 把word1的前i-1的字符变成word2的前j个字符，再删除word[i]。

public class Solution {
public int minDistance(String word1, String word2) {
int len1 = word1.length(),len2 = word2.length();
int[][] dp = new int[len1+1][len2+1];
for (int i = 1; i<=len1; i++)
dp[i][0] = i;
for (int j = 1; 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)) {
int min = Math.min(dp[i-1][j], Math.min(dp[i-1][j-1], dp[i][j-1]));
dp[i][j] = min+1;
}
else
dp[i][j] = dp[i-1][j-1];
}
}
return dp[len1][len2];

}
}

13ms,beats 63.16%，众数13ms,19.74%
Cmershen的碎碎念：

LeetCode(72)Edit Distance

2013-12-23 13:27:56

LeetCode 72 — Edit Distance（C++ Python）

2014-07-31 22:24:13

leetCode 72.Edit Distance (编辑距离) 解题思路和方法

2015-07-17 16:58:01

72 Edit Distance (编辑距离）

2017-07-23 16:48:14

LeetCode72——Edit Distance

2015-11-14 14:07:54

LeetCode Edit Distance 终极省内存方法

2013-12-22 08:13:57

Leetcode 72. Edit Distance 编辑距离 解题报告

2016-05-15 23:56:36

LeetCode-72. Edit Distance (JAVA)字符串最小编辑距离DP&DFS

2017-04-27 11:08:09

(Java)LeetCode-72. Edit Distance

2016-11-09 17:03:29

Edit Distance (Java)

2015-01-29 11:15:44