内容简介:【LeetCode】72. Edit Distance
问题描述
https://leetcode.com/problems/edit-distance/#/description
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
将 word1
变为 word2
,可以有三种操作,删除、替换、增加一个字符,每执行一次算作一次操作,返回至少需要多少次操作。
算法
使用动态规划,设 f(i,j)
表示将 word1
的前i个字符转化为 word2
的前j个字符,设 n=word1.length()
, m=word2.length()
,则最终的结果就是求: f(n, m)
下面是动态转移方程:
-
初始条件,
f(0,k) = f(k,0) = k,f(0,k)=k表示将word1的前0个字符转换为word2的前k个字符所需操作数,因为是从0个字符变换为k个字符,自然需要k次操作 -
word1[i] = word2[j]时,有f(i,j) = f(i-1,j-1),因为此时不需要操作,所以操作次数与前面的变换次数相等 -
word2[i] != word2[j]时,有f(i,j) = 1 + min{f(i,j-1),f(i-1,j),f(i-1,j-1)},f(i,j-1)表示insert,f(i-1,j)表示delete,f(i-1,j-1)表示replace
时间复杂度 O(nm)
代码
public class Solution {
/**
* 将word1变为word2,可以有三种操作,删除、替换、增加一个字符,每执行一次算作一次操作,返回至少需要多少次操作。
* 算法:
* 使用动态规划,设f(i,j)表示将word1的前i个字符转化为word2的前j个字符,设n=word1.length(), m=word2.length(),则最终的结果就是求:f(n, m)
* 下面是动态转移方程:
* 1. 初始条件,f(0,k) = f(k,0) = k,f(0,k)=k表示将word1的前0个字符转换为word2的前k个字符所需操作数,因为是从0个字符变换为k个字符,自然需要k次操作
* 2. word1[i] = word2[j]时,有f(i,j) = f(i-1,j-1),因为此时不需要操作,所以操作次数与前面的变换次数相等
* 3. word2[i] != word2[j]时,有f(i,j) = 1 + min{f(i,j-1), f(i-1,j), f(i-1,j-1)},f(i,j-1)表示insert, f(i-1,j)表示delete,f(i-1,j-1)表示replace
*/
public int minDistance(String word1, String word2) {
int n = word1.length();
int m = word2.length();
int[][] f = new int[n+1][m+1];
for(int i=0;i<=m;i++) {
f[0][i] = i;
}
for(int i=0;i<=n;i++) {
f[i][0] = i;
}
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
if(word1.charAt(i-1) == word2.charAt(j-1)) {
f[i][j] = f[i-1][j-1];
} else {
f[i][j] = 1 + Math.min(f[i][j-1], Math.min(f[i-1][j], f[i-1][j-1]));
}
}
}
return f[n][m];
}
}
转载请注明出处
:
http://www.zgljl2012.com/leetcode-72-edit-distance/
以上所述就是小编给大家介绍的《【LeetCode】72. Edit Distance》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Ant Colony Optimization
Marco Dorigo、Thomas Stützle / A Bradford Book / 2004-6-4 / USD 45.00
The complex social behaviors of ants have been much studied by science, and computer scientists are now finding that these behavior patterns can provide models for solving difficult combinatorial opti......一起来看看 《Ant Colony Optimization》 这本书的介绍吧!