Edit Distance
Problem Statement
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
This problem may be treated as an opertation problem. But in fact it's an counting problem about optimizing. That's why we'll consider using DP to solve it.
Define minimum number of steps convert word1[1:i] to word2[1:j].
The status transform equation.
In process of converting word1[] to word2[], the minimum solution must be one of the three converting method:
- At the last step, the operation is exactly converting character word1[] into character word2[], i.e. word1 and word2 terminate at the same time. So the transform equation should be: last step cost.
- Before reaching the end of word1, word1's beginning part has been converted into word2, i.e. word2 terminates in advance. So at the last step, the operation must be deleting the last character word1[i] where word1[1:(i-1)] has been converted into word2[1:j] in optimized way. So the equation should be:
.- Before reaching the end of word2, word1 has been converted into word2's beginning part, i.e. word1 terminates in advance. So the last step must be inserting the last character word2[j] into word1 where word1[1:i] has been converted into word2[1:(j-1)] in optimized way. So the equation should be
.
So the minimum steps of dp[i][j] is the minimum steps of above three statuses. One more thing need to notice is that, at first status, the last step cost depends on if word[i] is equal to word[j]. If it is, last step cost equals 0, else last step cost equals 1.
Coclude the above discussion as:
1 2 | lstCost = (word[i] == word[j]) ? 0 : 1; dp[i][j] = min(dp[i-1][j-1] + lstCost, dp[i-1][j] + 1, dp[i][j-1] + 1); |
The boundary condition is simple, just as
- convert from "" to "": ;
- convert from "" to word2[1:j]: ; Here,
- convert from word1[1:i] to "": ; Here,
The corresponding code should be:
1 2 3 4 5 6 | dp[0][0] = 0; for ( int i = 1; i <= word1.length(); ++i) dp[i][0] = i; for ( int j = 1; j <= word2.length(); ++j) dp[0][j] = j; |
The complete code is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | class Solution { public : int minDistance(string word1, string word2) { int i = 0; int steps = 0; while (word1[i] != 0 && word2[i] != 0){ if (word1[i] != word2[i]){ word1[i] = word2[i]; ++steps; } ++i; } if (word1[i] != 0){ steps += word1.end() - word1.begin() - i; word1.erase(word1.begin()+i, word1.end()); } if (word2[i] != 0){ word1 += word2[i]; ++i; ++steps; } return steps; } }; |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)