如何用编程计算两个字符串的最小编辑距离?
- 内容介绍
- 文章标签
- 相关推荐
本文共计652个文字,预计阅读时间需要3分钟。
使用一个二维数组dp[i][j]表示将第一个字符串的前i个字符与第二个字符串的前j个字符匹配所需的最少编辑步数。
1. 题目大意: 给定两个字符串,找出将其中一个字符串转换成另一个字符串所需的最少编辑步数。允许的编辑操作包括插入、删除和替换字符。
2. 标签: 动态规划
3. 题目链接: https://leetcode.cn/problems/edit-distance
使用一个二维数组dp[i][j],表示将第一个字符串到位置i为止,和第二个字符串到位置j为止,最多需要几步编辑。 一、题目大意标签: 动态规划
leetcode.cn/problems/edit-distance
给你两个单词word1 和word2, 请返回将word1转换成word2 所使用的最少操作数 。
你可以对一个单词进行如下三种操作:
插入一个字符
删除一个字符
替换一个字符
示例1:
输入:word1 = "horse", word2 = "ros"
输出:3
解释:
horse -> rorse (将 'h' 替换为 'r')
rorse -> rose (删除 'r')
rose -> ros (删除 'e')
示例2:
输入:word1 = "intention", word2 = "execution"
输出:5
解释:
intention -> inention (删除 't')
inention -> enention (将 'i' 替换为 'e')
enention -> exention (将 'n' 替换为 'x')
exention -> exection (将 'n' 替换为 'c')
exection -> execution (插入 'u')
提示:
- 0 <= word1.length, word2.length <= 500
- word1 和 word2 由小写英文字母组成
使用一个二维数组dp[i][j],表示将第一个字符串到位置i为止,和第二个字符串到位置j为止,最多需要几步编辑。当第i位和第j位对应的字符相同时,dp[i][j]等于dp[i-1][j-1];当二者对应的字符不同时,修改的消耗是dp[i-1][j-1]+1,插入i位置/删除j位置的消耗是dp[i][j-1]+1,插入j位置/删除i位置消耗是dp[i-1][j]+1。
三、解题方法 3.1 Java实现public class Solution {
public int minDistance(String word1, String word2) {
int m = word1.length();
int n = word2.length();
int[][] dp = new int[m + 1][n + 1];
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0) {
dp[i][j] = j;
} else if (j == 0) {
dp[i][j] = i;
} else {
dp[i][j] = Math.min(
dp[i - 1][j - 1] + (word1.charAt(i - 1) == word2.charAt(j - 1) ? 0 : 1),
Math.min(dp[i - 1][j] + 1, dp[i][j - 1] + 1)
);
}
}
}
return dp[m][n];
}
}
四、总结小记
- 2022/7/4 坚持每天一题
本文共计652个文字,预计阅读时间需要3分钟。
使用一个二维数组dp[i][j]表示将第一个字符串的前i个字符与第二个字符串的前j个字符匹配所需的最少编辑步数。
1. 题目大意: 给定两个字符串,找出将其中一个字符串转换成另一个字符串所需的最少编辑步数。允许的编辑操作包括插入、删除和替换字符。
2. 标签: 动态规划
3. 题目链接: https://leetcode.cn/problems/edit-distance
使用一个二维数组dp[i][j],表示将第一个字符串到位置i为止,和第二个字符串到位置j为止,最多需要几步编辑。 一、题目大意标签: 动态规划
leetcode.cn/problems/edit-distance
给你两个单词word1 和word2, 请返回将word1转换成word2 所使用的最少操作数 。
你可以对一个单词进行如下三种操作:
插入一个字符
删除一个字符
替换一个字符
示例1:
输入:word1 = "horse", word2 = "ros"
输出:3
解释:
horse -> rorse (将 'h' 替换为 'r')
rorse -> rose (删除 'r')
rose -> ros (删除 'e')
示例2:
输入:word1 = "intention", word2 = "execution"
输出:5
解释:
intention -> inention (删除 't')
inention -> enention (将 'i' 替换为 'e')
enention -> exention (将 'n' 替换为 'x')
exention -> exection (将 'n' 替换为 'c')
exection -> execution (插入 'u')
提示:
- 0 <= word1.length, word2.length <= 500
- word1 和 word2 由小写英文字母组成
使用一个二维数组dp[i][j],表示将第一个字符串到位置i为止,和第二个字符串到位置j为止,最多需要几步编辑。当第i位和第j位对应的字符相同时,dp[i][j]等于dp[i-1][j-1];当二者对应的字符不同时,修改的消耗是dp[i-1][j-1]+1,插入i位置/删除j位置的消耗是dp[i][j-1]+1,插入j位置/删除i位置消耗是dp[i-1][j]+1。
三、解题方法 3.1 Java实现public class Solution {
public int minDistance(String word1, String word2) {
int m = word1.length();
int n = word2.length();
int[][] dp = new int[m + 1][n + 1];
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0) {
dp[i][j] = j;
} else if (j == 0) {
dp[i][j] = i;
} else {
dp[i][j] = Math.min(
dp[i - 1][j - 1] + (word1.charAt(i - 1) == word2.charAt(j - 1) ? 0 : 1),
Math.min(dp[i - 1][j] + 1, dp[i][j - 1] + 1)
);
}
}
}
return dp[m][n];
}
}
四、总结小记
- 2022/7/4 坚持每天一题

