[Leetcode] 72. Edit Distance 解题报告
来源:互联网 发布:it 域名 编辑:程序博客网 时间:2024/06/04 19:38
题目:
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[0, i]到word2[0, j]的编辑距离,根据对最后一个字符的处理,可以有如下两种情况:
1)如果word1[i] == word2[j],那么不用处理最后一个字符,其最优编辑过程也就是从word1[0, i-1]到word2[0, j-1]的编辑过程。此时dp[i][j] = dp[i-1][j-1]。
2)如果word1[i-1] != word2[j-1],那么根据对最后一个字符的处理分类,有如下三种可能:
a、替换:此时对应的编辑过程就是:step 1: 将word1[0, i-1]编辑为word2[0, j-1];step 2: 将word1[i]替换为word2[j]。此时dp[i][j] = dp[i-1][j-1] + 1。
b、插入:此时对应的编辑过程就是:step 1: 将word1[0, i]编辑为word2[0, j-1];step 2: 在word2[0, j-1]的末尾加上words[j]。此时dp[i][j] = dp[i][j-1] + 1。
c、删除:此时对应的编辑过程就是:step 1: 将word1[0, i-1]编辑为word2[0, j];step 2: 将word1[i]删除。此时dp[i][j] = dp[i-1][j] + 1。
注意在上述三种情况中,step 1和step 2的执行次序可以调换。
分析可知,dp[i][j]依然只和它左方、上方以及左上方的三个元素有关,因此空间复杂度可以采用动态规划的方式优化到O(len2),其中len1和len2分别是字符串word1和word2的长度。如果对空间复杂度比较在意,还可以进一步将空间复杂度优化为O(min(len1, len2)) (适合len2远大于len1的情况,此时虽然理论上程序的时间复杂度仍然为O(len1 * len2),但是运行时间却会增加不少,请思考一下为什么?O(∩_∩)O)。
代码:
1、O(len1 * len2)空间复杂度的DP解法:
class Solution {public: int minDistance(string word1, string word2) { int len1 = word1.length(), len2 = word2.length(); vector<vector<int>> dp(len1 + 1, vector<int>(len2 + 1, 0)); for(int i = 0; i <= len1; ++i) // initialize the first column dp[i][0] = i; for(int i = 0; i <= len2; ++i) // initialize the first row dp[0][i] = i; for(int i = 1; i <= len1; ++i) { for(int j = 1; j <= len2; ++j) { if(word1[i-1] == word2[j-1]) dp[i][j] = dp[i-1][j-1]; else dp[i][j] = min(min(dp[i][j-1], dp[i-1][j]), dp[i-1][j-1]) + 1; } } return dp[len1][len2]; }};
2、O(min(len1, len2))空间复杂度的DP解法:
class Solution {public: int minDistance(string word1, string word2) { if(word1.length() < word2.length()) { swap(word1, word2); // optional: may decrease space complexity while increasing running time } int len1 = word1.length(), len2 = word2.length(); vector<vector<int>> dp(2, vector<int>(len2 + 1, 0)); for(int i = 0; i <= len2; ++i) // initialize the first column dp[0][i] = i; for(int i = 1; i <= len1; ++i) { dp[i%2][0] = i; for(int j = 1; j <= len2; ++j) { if(word1[i-1] == word2[j-1]) dp[i%2][j] = dp[(i-1)%2][j-1]; else dp[i%2][j] = min(min(dp[i%2][j-1], dp[(i-1)%2][j]), dp[(i-1)%2][j-1]) + 1; } } return dp[len1%2][len2]; }};
- [leetcode] 72. Edit Distance 解题报告
- [Leetcode] 72. Edit Distance 解题报告
- 【LeetCode】72.Edit Distance解题报告
- [LeetCode] Edit Distance 解题报告
- Edit Distance [Leetcode解题报告]
- Leetcode 72. Edit Distance 编辑距离 解题报告
- [leetcode] One Edit Distance 解题报告
- [Leetcode] 161. One Edit Distance 解题报告
- Edit Distance 解题报告
- LeetCode 72. Edit Distance
- [LeetCode]72.Edit Distance
- LeetCode --- 72. Edit Distance
- [Leetcode] 72. Edit Distance
- [leetcode] 72.Edit Distance
- [leetcode] 72.Edit Distance
- Leetcode 72. Edit Distance
- LeetCode 72. Edit Distance
- leetcode 72. Edit Distance
- [Leetcode] 71. Simplify Path 解题报告
- PHP弱类型--strcmp()
- 九度OJ-1172-哈夫曼树
- 748UVa迷宫探索
- UVa 1589 象棋 xiangqi 问题。 判断是否将军致死。
- [Leetcode] 72. Edit Distance 解题报告
- 构建本地vim编程环境
- iOS 沙盒文件路径获取
- Effective Java 读书笔记——39:必要时进行保护性拷贝
- [Leetcode] 73. Set Matrix Zeroes 解题报告
- 区间找点
- CCF201403-2 窗口(100分)
- LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)
- 微信自研生产级 Paxos 类库 PhxPaxos 实现原理介绍