当前位置 : 首页 » 文章分类 :  算法  »  LeetCode.072.Edit Distance 编辑距离

LeetCode.072.Edit Distance 编辑距离

题目描述

72 Edit Distance
https://leetcode-cn.com/problems/edit-distance/

Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2.

You have the following 3 operations permitted on a word:

  1. Insert a character
  2. Delete a character
  3. Replace a character

Example 1:

Input: word1 = "horse", word2 = "ros"
Output: 3
Explanation:
horse -> rorse (replace 'h' with 'r')
rorse -> rose (remove 'r')
rose -> ros (remove 'e')

Example 2:

Input: word1 = "intention", word2 = "execution"
Output: 5
Explanation:
intention -> inention (remove 't')
inention -> enention (replace 'i' with 'e')
enention -> exention (replace 'n' with 'x')
exention -> exection (replace 'n' with 'c')
exection -> execution (insert 'u')

解题过程

经典的动态规划题
dist[i][j] 表示 word1[0...i-1] 转换为 word2[0...j-1] 的最小操作次数,也就是 word1 的前 i 个字符转换为 word2 的前 j 个字符需要的最小操作次数,则:
i == 0 时, 表示 word1 是空串, 则 dist[i][j] = j
j == 0 时, 表示 word2 是空串, 则 dist[i][j] = i
word1[i] == word2[j] 时, dist[i][j] = dist[i-1][j-1]
word1[i] != word2[j] 时, min(dist[i-1][j] + 1, dist[i][j-1] + 1, dist[i-1][j-1] + 1)

$ dist[i, j] =
\begin{cases}
j, & \text{ i == 0 } \\
i, & \text{ j == 0 } \\
dist[i-1, j-1], & \text{ word1[i] == word2[j] } \\
min(dist[i-1, j-1], dist[i-1][j], dist[i][j-1]) + 1, & \text{ word1[i] != word2[j] }
\end{cases}
$

private static class SolutionV2020 {
    public int minDistance(String word1, String word2) {
        if (null == word1 && null == word2) {
            return 0;
        }
        if (null == word1 || null == word2) {
            return null == word1 ? word2.length() : word1.length();
        }
        if (word1.equals(word2)) {
            return 0;
        }
        // dist[i][j] 表示 word1[0...i-1] 转换为 word2[0...j-1] 的最小操作次数,则
        // word1[i] == word2[j] 时, dist[i][j] = dist[i-1][j-1]
        // word1[i] != word2[j] 时, min(dist[i-1][j]+1, dist[i][j-1]+1, dist[i-1][j-1]+1)
        int[][] dist = new int[word1.length() + 1][word2.length() + 1];

        // word2 为空串
        for (int i = 0; i <= word1.length(); i++) {
            dist[i][0] = i;
        }
        // word1 为空串
        for (int j = 0; j <= word2.length(); j++) {
            dist[0][j] = j;
        }

        for (int i = 0; i < word1.length(); i++) {
            for (int j = 0; j < word2.length(); j++) {
                if (word1.charAt(i) == word2.charAt(j)) {
                    dist[i+1][j+1] = dist[i][j];
                } else {
                    dist[i+1][j+1] = Math.min(dist[i][j+1], Math.min(dist[i+1][j], dist[i][j])) + 1;
                }
            }
        }
        return dist[word1.length()][word2.length()];
    }
}

GitHub代码

algorithms/leetcode/leetcode/_072_EditDistance.java
https://github.com/masikkk/algorithms/blob/master/leetcode/leetcode/_072_EditDistance.java


上一篇 LeetCode.程序员面试金典.0107.Rotate Matrix 旋转矩阵

下一篇 LeetCode.460.LFU Cache 实现LFU缓存

阅读
评论
545
阅读预计3分钟
创建日期 2020-04-06
修改日期 2020-04-06
类别

页面信息

location:
protocol:
host:
hostname:
origin:
pathname:
href:
document:
referrer:
navigator:
platform:
userAgent:

评论