2014.2.25 23:07
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
Solution1:
This is a typical problem of dynamic programming. Let f[i][j] be the edit distance of w1[1:i] and w2[1:j]:
1. f[0][0] = 0, all empty
2. f[i][0] = i, delete i letters
3. f[0][j] = j, insert j letters
4. f[i][j] could be f[i][j - 1] + 1, insert a letter.
5. f[i][j] could be f[i - 1][j] + 1, delete a letter.
6. If w1[i] == w2[j], f[i][j] could be f[i - 1][j - 1].
7. If w1[i] != w2[j], f[i][j] could be f[i - 1][j - 1] + 1, replace a letter.
f[i][j] will be the minimum of them all. The result is f[len1][len2].
The solution uses O(n^2) time and space.
Accepted code:
1 // 2CE, 2WA, 1AC, standard DP problem. 2 #include <algorithm> 3 #include <string> 4 using namespace std; 5 6 class Solution { 7 public: 8 int minDistance(string word1, string word2) { 9 int n1, n2; 10 11 if (n1 < n2) { 12 return minDistance(word2, word1); 13 } 14 15 n1 = (int)word1.length(); 16 n2 = (int)word2.length(); 17 if (n1 == 0) { 18 return n2; 19 } else if (n2 == 0) { 20 return n1; 21 } 22 23 int **dp; 24 25 dp = new int*[n1 + 1]; 26 dp[0] = new int[(n1 + 1) * (n2 + 1)]; 27 28 int i, j; 29 for (i = 1; i <= n1; ++i) { 30 dp[i] = dp[0] + (i * (n2 + 1)); 31 } 32 33 dp[0][0] = 0; 34 for (i = 1; i <= n1; ++i) { 35 dp[i][0] = i; 36 } 37 for (j = 1; j <= n2; ++j) { 38 dp[0][j] = j; 39 } 40 41 for (i = 1; i <= n1; ++i) { 42 for (j = 1; j <= n2; ++j) { 43 dp[i][j] = min(dp[i - 1][j] , dp[i][j - 1]) + 1; 44 if (word1[i - 1] == word2[j - 1]) { 45 dp[i][j] = min(dp[i][j], dp[i - 1][j - 1]); 46 } else { 47 dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + 1); 48 } 49 } 50 } 51 int result = dp[n1][n2]; 52 53 delete[] dp[0]; 54 delete[] dp; 55 56 return result; 57 } 58 };
Solution2:
Obviously the space usage can be reduced to O(n) with proper adjustment.
See the code below.
Accepted code:
1 // 1AC, space-optimized version using DP. 2 #include <algorithm> 3 #include <string> 4 using namespace std; 5 6 class Solution { 7 public: 8 int minDistance(string word1, string word2) { 9 int n1, n2; 10 11 if (n1 < n2) { 12 return minDistance(word2, word1); 13 } 14 15 n1 = (int)word1.length(); 16 n2 = (int)word2.length(); 17 if (n1 == 0) { 18 return n2; 19 } else if (n2 == 0) { 20 return n1; 21 } 22 23 int **dp; 24 25 dp = new int*[2]; 26 dp[0] = new int[2 * (n2 + 1)]; 27 dp[1] = dp[0] + (n2 + 1); 28 29 int i, j; 30 31 for (j = 0; j <= n2; ++j) { 32 dp[0][j] = j; 33 } 34 35 int flag = 1; 36 for (i = 1; i <= n1; ++i) { 37 dp[flag][0] = i; 38 for (j = 1; j <= n2; ++j) { 39 dp[flag][j] = min(dp[!flag][j] , dp[flag][j - 1]) + 1; 40 if (word1[i - 1] == word2[j - 1]) { 41 dp[flag][j] = min(dp[flag][j], dp[!flag][j - 1]); 42 } else { 43 dp[flag][j] = min(dp[flag][j], dp[!flag][j - 1] + 1); 44 } 45 } 46 flag = !flag; 47 } 48 int result = dp[!flag][n2]; 49 50 delete[] dp[0]; 51 delete[] dp; 52 53 return result; 54 } 55 };
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)