leetcode || 72、Edit Distance

problem:

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

Hide Tags

Dynamic Programming String

题意:求将字符串word1 转换为word2 所需最小的步数,操作包括插入、替代和删除一个字符

thinking:

(1)求全局最优解,锁定DP法

(2)DP的状态转移公式不好找,有几点是DP法共有的,可以有点启发:1、DP大都借助数组实现递推操作 2、DP法的时间复杂度:一维为O(N),二维:O(M*N)

(3)

如果我们用 i 表示当前字符串 A 的下标,j 表示当前字符串 B 的下标。 如果我们用d[i, j] 来表示A[1, ... , i] B[1, ... , j] 之间的最少编辑操作数。那么我们会有以下发现:

1. d[0, j] = j;

2. d[i, 0] = i;

3. d[i, j] = d[i-1, j - 1] if A[i] == B[j]

4. d[i, j] = min(d[i-1, j - 1], d[i, j - 1], d[i-1, j]) + 1  if A[i] != B[j]  //分别代表替换、插入、删除

code:

class Solution {
public:
    int minDistance(string word1, string word2) {
        vector<vector<int> > f(word1.size()+1, vector<int>(word2.size()+1));

        f[0][0] = 0;
        for(int i = 1; i <= word2.size(); i++)
            f[0][i] = i;

        for(int i = 1; i <= word1.size(); i++)
            f[i][0] = i;

        for(int i = 1; i <= word1.size(); i++)
            for(int j = 1; j <= word2.size(); j++)
            {
                f[i][j] = INT_MAX;
                if (word1[i-1] == word2[j-1])
                    f[i][j] = f[i-1][j-1];

                f[i][j] = min(f[i][j], f[i-1][j-1] + 1); //replace
                f[i][j] = min(f[i][j], min(f[i-1][j], f[i][j-1]) + 1); //delete or insert
            }

        return f[word1.size()][word2.size()];
    }
};
时间: 2024-08-25 23:04:54

leetcode || 72、Edit Distance的相关文章

(LeetCode 72)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 characterc) Replace

[Leetcode 72]编辑距离 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: Insert a character Delete a character Replace a character Example 1: Input: word1

【LeetCode 72】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 characterc) Replace

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

[email&#160;protected] [72/115] Edit Distance &amp; Distinct Subsequences (Dynamic Programming)

https://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 word: a) Inse

leetcode[161] One Edit Distance

判断两个字符串的编辑距离是不是1. 两个字符串距离是1的可能性: 1)两个字符串长度相等:s="abc",t="aec"; 2)两个字符串长度相差1(两种情况):s="abc",t="abce";或s="abc",t="aebc"; bool isOneEditDistance(string s, string t) { if(s.length()>t.length())swap(

LeetCode – Refresh – One Edit Distance

Scanning from start to end. If find a mismatch and one is larger size, keep search from the previous char of shorter one. Finally check whether found a mismatch OR still have a larger size string. 1 class Solution { 2 public: 3 bool isOneEditDistance

leetcode day4 -- Binary Tree Postorder(Preorder) Traversal &amp;&amp; Edit Distance

 1.Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 分析:后续遍历

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