(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 character
b) Delete a character
c) Replace a character

题目:

给定两个字符串,求把S变成T所需的最小操作数。

3种字符操作分别为插入、删除、替换。

思路:

动态规划思想:

假设dp[i][j]表示以S[i]结尾的字符串和以T[j]结尾的字符串转换所需的最小操作数,考虑三种操作,然后取三者最小值:

1、替换:

假设S[i-1],T[j-1]已对齐,即dp[i-1][j-1]已知,则当S[i]==T[j]时,dp[i][j]=dp[i-1][j-1],否则,dp[i][j]=dp[i-1][j-1]+1.

2、删除

假设S[i-1],T[j]已对齐,即dp[i-1][j]已知,多出来的S[i]需删除,操作数+1,则dp[i][j]=dp[i-1][j]+1.

3、插入

假设S[i],T[j-1]已对齐,即dp[i][j-1]已知,需在S中插入S[i+1]=T[j]来匹配,操作数+1,则dp[i][j]=dp[i][j-1]+1.

状态转移方程:

dp[i][j]=min(dp[i-1][j-1]+(S[i]==T[j]?0,1),dp[i-1][j]+1,dp[i][j-1]+1)

初始值:

dp[i][0]=i

dp[0][j]=j

复杂度:

时间复杂度:O(m*n)

空间复杂度:O(m*n)

空间优化:

由状态转移方程可知,dp[i][j]与dp[i-1][j-1],dp[i-1][j],dp[i][j-1]有关,可以去掉一维,只留下dp[j]。

等式右边的dp[i-1][j]和dp[i][j-1]都可以直接改成dp[j](旧的值)和dp[j-1](已更新),只有dp[i-1][j-1]没有记录下来,通过某个变量保存起来之后就可以。

因此空间复杂度:O(n)

代码:

class Solution {
public:
    int minDistance(string word1, string word2) {
        int m=word1.length();
        int n=word2.length();
        vector<vector<int> > distance(m+1,vector<int>(n+1));

        for(int i=0;i<=m;i++){
            for(int j=0;j<=n;j++){
                if(0==i){
                    distance[i][j]=j;
                }
                else if(0==j){
                    distance[i][j]=i;
                }
                else{
                    distance[i][j]=min(distance[i-1][j-1]+((word1[i-1]==word2[j-1])?0:1),
                                       min(distance[i-1][j]+1,distance[i][j-1]+1)
                                       );
                }
            }
        }
        return distance[m][n];
    }
};
class Solution {
public:
    int minDistance(string word1, string word2) {
        int m=word1.length();
        int n=word2.length();
        vector<int> distance(n+1);

        for(int i=0;i<=m;i++){
            int last;
            for(int j=0;j<=n;j++){
                if(0==i){
                    distance[j]=j;
                }
                else if(0==j){
                    last=distance[j];
                    distance[j]=i;
                }
                else{
                    int temp=distance[j];
                    distance[j]=min(last+((word1[i-1]==word2[j-1])?0:1),
                                       min(distance[j]+1,distance[j-1]+1)
                                       );
                    last=temp;
                }
            }
        }
        return distance[n];
    }
};

  

时间: 2025-01-31 06:44:32

(LeetCode 72)Edit Distance的相关文章

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

[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题解)Pascal&#39;s Triangle

Pascal's Triangle  Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题意实现一个杨辉三角. 这道题只要注意了边界条件应该很好实现出来,C++实现如下 vector<vector<int>> generate(int

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

ZigZag问题的一种新思路(Leetcode #6)

原始问题(leetcode ZigZag Conversion) 如果固定一个行数(这里是3),字符串 "PAYPALISHIRING" 用"之"字形的方式可以写成: P A H N A P L S I I G Y I R 这个结果按照一行一行访问的话是: "PAHNAPLSIIGYIR".给定一个字符串以及一个表示行数的整数,实现一个函数,返回按行序拼接而成的新串. string convert(string text, int nRows);

(LeetCode 78)SubSets

Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. 题目要求 :求整数数组的所有子集 注意: 1.子集元素按非降序排列 2.不包含重复的子集 解题思路: 求解这类诸如子集的题目,都可以采用回溯法

(leetcode题解)Reshape the Matrix

In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data. You're given a matrix represented by a two-dimensional array, and two positive integers r and c rep

(leetcode题解)Can Place Flowers

Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and both would die. Given a flowerbed (represented as an array containing 0