Leetcode:Edit Distance 字符串编辑距离

原题戳我

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

解析:

典型动态规划题.

设 distance[i,j]表示 word1[i]和word2[j]之间的编辑距离

1. 当word1[i] == word2[j]时,则 distance[i,j] = distance[i-1, j-1]

2. 当word1[i] 不等于 word2[j]时,分三种情况,分别对应三种操作

word1插入1个字符,即 distance[i, j] = distance[i, j - 1] + 1
word1删除1个字符,即 distance[i, j] = distance[i - 1, j] + 1
word1替换1个字符,即 distance[i, j] = distance[i - 1, j - 1] + 1
class Solution {
public:
    int minDistance(string word1, string word2) {
        if (word1.size() == 0) return word2.size();
        if (word2.size() == 0) return word1.size();

        int rowNum = word1.size() + 1;
        int colNum = word2.size() + 1;
        std::vector<std::vector<int>> distance(rowNum, std::vector<int>(colNum, 0));
        for (int i = 0; i < rowNum; ++i) {
            distance.at(i).at(0) = i;
        }
        for (int j = 0; j < colNum; ++j) {
            distance.at(0).at(j) = j;
        }

        for (int i = 1; i < rowNum; ++i) {
            for (int j = 1; j < colNum; ++j) {
                if (word1.at(i - 1) == word2.at(j - 1)) {
                    distance.at(i).at(j) = distance.at(i - 1).at(j - 1);
                } else {
                    int deleteCost = distance.at(i - 1).at(j) + 1;
                    int insertCost = distance.at(i).at(j - 1) + 1;
                    int replaceCost = distance.at(i - 1).at(j - 1) + 1;
                    distance.at(i).at(j) = std::min(deleteCost, std::min(insertCost, replaceCost));
                }
            }
        }
        return distance.at(word1.size()).at(word2.size());
    }
};

注意:

distance二维矩阵的设定,行和列都是对应字符串长度加1

distance二维矩阵的 0行 和 0列的初始化,边界情况

时间: 2024-12-05 18:18:25

Leetcode:Edit Distance 字符串编辑距离的相关文章

[LeetCode] Edit Distance(很好的DP)

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) Repla

[LeetCode] One Edit Distance 一个编辑距离

Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance的拓展,然而这道题并没有那道题难,这道题只让我们判断两个字符串的编辑距离是否为1,那么我们只需分下列三种情况来考虑就行了: 1. 两个字符串的长度之差大于1,那么直接返回False 2. 两个字符串的长度之差等于1,那么长的那个字符串去掉一个字符,剩下的应该和短的字符串相同 3. 两个字符串的长度之

Leetcode:Edit Distance 解题报告

Edit Distance 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 characterb) Delete a chara

LeetCode:Edit Distance(字符串编辑距离DP)

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) Repla

[leetcode]Edit Distance @ Python

原题地址:https://oj.leetcode.com/problems/edit-distance/ 题意: 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 w

LeetCode - Edit Distance

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) Repla

LeetCode 72. Edit Distance Java

72.Edit Distance(编辑距离) 题目: 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 思路: 多次选择试图得到最优解,那么考虑动态规划. 先假设word1有len1位,word2有len2位,建立数组step,step[i][j]就代表我们要将word1前 i 位转换为word2前 j 位的最少数量. 此时word1查找到第 i+1 位字母a,

leetcode 161. One Edit Distance 判断两个字符串是否是一步变换 --------- java

Given two strings S and T, determine if they are both one edit distance apart. 给定两个字符串,判断他们是否是一步变换得到的. 在这里需要注意几点: 1.不等于1的变换都要返回false(包括变换次数等于0). 2.还有很多细节需要注意. 方法如下: 1.直接判断:1)如果差值大于1,直接返回false.  2)如果长度相同,那么依次判断,是否只有一个字母不一样.  3)如果不一样,那么看是否是只是多出了一个字母. p

leetCode 72.Edit Distance (编辑距离) 解题思路和方法

Edit Distance 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 char