LeetCode 72. Edit Distance(编辑距离)
2018-03-26 15:05
483 查看
题目描述:
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,每次可以进行一步操作:① 在word1中插入一个字符;② 在word1中删除一个字符;③ 在word1中修改一个字符。返回将word1转化为word2的最小步数。
思路:这是一道经典的动态规划题,编辑距离。假设两个字符串的长度为n1、n2,我们构造数组dp[n1 + 1][n2 + 1],dp[i][j]表示从word1[0→i - 1]转化为word2[0→j - 1]需要的最小步数。初始化各元素值为0(其中dp[0][0] = 1,dp[i][0] = 1(i∈1→n1) 表示删除word1中前i个元素可以匹配空串word2,dp[0][j] = 1(j∈1→n2) 表示在空串word1中加上word2的前j个元素可以匹配word2[0→j - 1])。
那么对于一般情况word1[0→i - 1])和word2[0→j - 1]),分为两种情况:Ⅰ. 如果word1[i - 1]等于word2[j - 1],则取决于dp[i - 1][j - 1]的值;Ⅱ. 如果不相等,可以通过增、删、改三种操作判断是否匹配,状态转移方程为:dp[i][j] = min(dp[i - 1][j - 1], min(dp[i][j - 1], dp[i - 1][j])) + 1。
时间复杂度为O(n1 * n2)。
代码:
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int minDistance(string word1, string word2) {
int n1 = word1.length(), n2 = word2.size();
// Exceptional Case:
if(n1 == 0){
return n2;
}
if(n2 == 0){
return n1;
}
// create
vector<vector<int>> dp(n1 + 1, vector<int>(n2 + 1, 0));
// init
dp[0][0] = 0;
for(int i = 1; i <= n1; i++){
dp[i][0] = i;
}
for(int i = 1; i <= n2; i++){
dp[0][i] = i;
}
// dp
for(int i = 1; i <= n1; i++){
for(int j = 1; j <= n2; j++){
if(word1[i - 1] == word2[j - 1]){
dp[i][j] = dp[i - 1][j - 1];
}
else{
dp[i][j] = min(dp[i - 1][j - 1], min(dp[i][j - 1], dp[i - 1][j])) + 1;
}
}
}
return dp[n1][n2];
}
};
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,每次可以进行一步操作:① 在word1中插入一个字符;② 在word1中删除一个字符;③ 在word1中修改一个字符。返回将word1转化为word2的最小步数。
思路:这是一道经典的动态规划题,编辑距离。假设两个字符串的长度为n1、n2,我们构造数组dp[n1 + 1][n2 + 1],dp[i][j]表示从word1[0→i - 1]转化为word2[0→j - 1]需要的最小步数。初始化各元素值为0(其中dp[0][0] = 1,dp[i][0] = 1(i∈1→n1) 表示删除word1中前i个元素可以匹配空串word2,dp[0][j] = 1(j∈1→n2) 表示在空串word1中加上word2的前j个元素可以匹配word2[0→j - 1])。
那么对于一般情况word1[0→i - 1])和word2[0→j - 1]),分为两种情况:Ⅰ. 如果word1[i - 1]等于word2[j - 1],则取决于dp[i - 1][j - 1]的值;Ⅱ. 如果不相等,可以通过增、删、改三种操作判断是否匹配,状态转移方程为:dp[i][j] = min(dp[i - 1][j - 1], min(dp[i][j - 1], dp[i - 1][j])) + 1。
时间复杂度为O(n1 * n2)。
代码:
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int minDistance(string word1, string word2) {
int n1 = word1.length(), n2 = word2.size();
// Exceptional Case:
if(n1 == 0){
return n2;
}
if(n2 == 0){
return n1;
}
// create
vector<vector<int>> dp(n1 + 1, vector<int>(n2 + 1, 0));
// init
dp[0][0] = 0;
for(int i = 1; i <= n1; i++){
dp[i][0] = i;
}
for(int i = 1; i <= n2; i++){
dp[0][i] = i;
}
// dp
for(int i = 1; i <= n1; i++){
for(int j = 1; j <= n2; j++){
if(word1[i - 1] == word2[j - 1]){
dp[i][j] = dp[i - 1][j - 1];
}
else{
dp[i][j] = min(dp[i - 1][j - 1], min(dp[i][j - 1], dp[i - 1][j])) + 1;
}
}
}
return dp[n1][n2];
}
};
相关文章推荐
- [LeetCode] 72. Edit Distance 编辑距离
- LeetCode-72. Edit Distance (JAVA)字符串最小编辑距离DP&DFS
- Leetcode 72. Edit Distance 编辑距离 解题报告
- LeetCode 72. Edit Distance(编辑距离)
- LeetCode刷题 | 72. Edit Distance 难题 编辑距离dp
- leetcode:Edit Distance 编辑距离
- [LeetCode] One Edit Distance 一个编辑距离
- 72. Edit Distance 两个单词的编辑距离
- leetCode 72.Edit Distance (编辑距离) 解题思路和方法
- 72. Edit Distance 编辑距离
- leetCode 72.Edit Distance (编辑距离) 解题思路和方法
- Leetcode 72. Edit Distance (Hard) (cpp)
- [leetcode] 72. Edit Distance
- [leetcode]72. Edit Distance
- [LeetCode] 245. Shortest Word Distance III 最短单词距离 III
- LeetCode 72. Edit Distance
- Minimum edit distance(levenshtein distance)(最小编辑距离)初探
- LeetCode 72. Edit Distance-----Python实现
- 【LeetCode】72. Edit Distance
- LeetCode 161. One Edit Distance(编辑距离)