【Lintcode】119.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:

  • Insert a character
  • Delete a character
  • Replace a character

Example

Given word1 = "mart" and word2 = "karma", return 3.

题解:

Solution 1 ()

class Solution {
public:
    int minDistance(string word1, string word2) {
        int len1 = word1.size(), len2 = word2.size();
        vector<vector<int> > dp(len1 + 1, vector<int> (len2 + 1));
        for (int i = 0; i <= len1; ++i) {
            dp[i][0] = i;
        }
        for (int i = 0; i <= len2; ++i) {
            dp[0][i] = i;
        }
        for (int i = 1; i <= len1; ++i) {
            for (int j = 1; j <= len2; ++j) {
                if (word1[i - 1] == word2[j - 1]) {
                    dp[i][j] = dp[i - 1][j - 1];
                } else {
                    dp[i][j] = min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])) + 1;
                }
            }
        }
        return dp[len1][len2];
    }
};
时间: 2024-11-05 12:20:54

【Lintcode】119.Edit Distance的相关文章

【LeetCode】119 - Pascal&#39;s Triangle II

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? Solution: 1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex){

(分类讨论, 情景模拟) lintcode 640. One Edit Distance, leetcode 388,intcode 645. 13. 12. 659. 660

找特殊情况,分类讨论:三种情况 1)两个字符串的长度之差 大于1 直接返回false: 2)长度之差等于1, 判断长的字符串删掉不一样的字符,剩余的字符串是否相同: 3)长度之差等于0,判断不相同的字符个数,若超过一个返回false. class Solution { public: /** * @param s: a string * @param t: a string * @return: true if they are both one edit distance apart or f

【一天一道LeetCode】#72. Edit Distance

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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 f

【Lintcode】074.First Bad Version

题目: The code base version is an integer start from 1 to n. One day, someone committed a bad version in the code case, so it caused this version and the following versions are all failed in the unit tests. Find the first bad version. You can call isBa

【Lintcode】Median of two Sorted Arrays

class Solution { public: /** * @param A: An integer array. * @param B: An integer array. * @return: a double whose format is *.5 or *.0 */ int check(vector<int>& A, vector<int>& B,int k,int m) { int M = A.size(); int N = B.size(); int

【UVA】11796 - Dog Distance(相对运动)

因为a,b是同时出发,同时达到,但是他们的速度不一定一样,所以我们可以设他们的速度为La,Lb(La,Lb为a,b狗的总路程) 那么,如何a,b都是沿着直线运动的时候如何求他们之间的最短最长距离呢? 因为运动都是相对的,所以我们可以把a看成不懂的,而b相对于a的移动方向就是Vb - Va,因此就可以看成a和线段 b + (Vb -Va)之间的关系了 至于方向Va,Vb向量怎么求,我们可以利用单位向量 X 移动时间 X 移动速度 得到. 经典的一道题,顺便还修复了一个模板的BUG: 1412818

【lintcode】Count of Smaller Number before itself

http://www.lintcode.com/en/problem/count-of-smaller-number-before-itself/ 这道题目是动态添加线段树的元素,然后再查询. 数据集和描述不相符,坑 class Solution { public: /** * @param A: An integer array * @return: Count the number of element before this element 'ai' is * smaller than i

【Leetcode_easy】849. Maximize Distance to Closest Person

problem 849. Maximize Distance to Closest Person 参考 1. Leetcode_easy_849. Maximize Distance to Closest Person; 完 原文地址:https://www.cnblogs.com/happyamyhope/p/11214905.html

【leetcode】1320. Minimum Distance to Type a Word Using Two Fingers

题目如下: You have a keyboard layout as shown above in the XY plane, where each English uppercase letter is located at some coordinate, for example, the letter A is located at coordinate (0,0), the letter B is located at coordinate (0,1), the letter P is