72. Edit Distance && 161. One Edit Distance

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

Dynamic Programming String

Classic DP problem. DP feels like mathematical induction

/**
 * dp[r][c] indicates the minimum number of operations to convert word1[0,r] to word2[0,c]
 * boundary conditions:
 * dp[r][0] = r
 * dp[0][c] = c
 * <p>
 * if word1[r] == word2[c]:  dp[r][c] = dp[r-1][c-1]
 * else:  dp[r][c] = min(dp[r-1][c-1]+1, dp[r-1][c]+1, dp[r][c-1]+1)
 * <p>
 * dp[r-1][c-1]+1 means "replace the last character of word1 with the last character of word2"
 * dp[r-1][c]+1 means "delete the last character from word1":
 *    dp[r-1][c] already matches. The extra word1[r] is useless, so we remove it
 * dp[r][c-1]+1 means "add the last character of word2 to the end of word1":
 *    dp[r][c-1] already matches. In order to match word2[c], we need to add it to the end of word1
 */
class Solution {
  public int minDistance(String word1, String word2) {
    int ROW = word1.length();
    int COL = word2.length();
    int[][] dp = new int[ROW + 1][COL + 1];
    for (int r = 1; r <= ROW; ++r)
      dp[r][0] = r;
    for (int c = 1; c <= COL; ++c)
      dp[0][c] = c;

    for (int r = 1; r <= ROW; ++r) {
      for (int c = 1; c <= COL; ++c) {
        if (word1.charAt(r - 1) == word2.charAt(c - 1))
          dp[r][c] = dp[r - 1][c - 1];
        else
          dp[r][c] = Math.min(dp[r - 1][c - 1] + 1, Math.min(dp[r][c - 1] + 1, dp[r - 1][c] + 1));
      }
    }
    return dp[ROW][COL];
  }
}

161. One Edit Distance

Given two strings S and T, determine if they are both one edit distance apart.

时间: 2024-12-15 15:08:15

72. Edit Distance && 161. One Edit Distance的相关文章

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

161. One Edit Distance

题目: Given two strings S and T, determine if they are both one edit distance apart. 链接: http://leetcode.com/problems/one-edit-distance/ 6/14/2017 2ms, 58% 提交好几次才通过,所以代码像打补丁一样.注意的test case: 2个空string -- false 相同的string -- false 当检查出来不一致时,根据长度来比较之后的subs

【LeetCode】161. One Edit Distance

Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given two strings S and T, determine if they are both one edit distance apart. Intuition 同时遍历比较S和T的字符,直至遇到不同的字符:如果S和T的字符个数相同时,跳过不同的那个字符,继续遍历:如果S和T的字符个数相差为1时,跳过较长的字符串的当天字符,继续遍历.如果剩下的字符都相等,那么返回tr

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

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

(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 One Edit Distance

原题链接在这里:https://leetcode.com/problems/one-edit-distance/ Given two strings S and T, determine if they are both one edit distance apart. 与Edit Distance类似. 若是长度相差大于1, return false. 若是长度相差等于1, 遇到不同char时, 长的那个向后挪一位. 若是长度相等, 遇到不同char时同时向后挪一位. 出了loop还没有返回,

最小编辑距离(Minimum edit distance)

最小编辑距离是计算欧式距离的一种方法,可以被用于计算文本的相似性以及用于文本纠错,因为这个概念是俄罗斯科学家 Vladimir Levenshtein 在1965年提出来的,所以编辑距离又称为Levenshtein距离. 简单的理解就是将一个字符串转换到另一个字符串所需要的代价(cost),付出的代价越少表示两个字符串越相似,编辑距离越小,从一个字符串转换到另一个字符串简单的归纳可以有以下几种操作,1.删除(delete)2.插入(insert)3.修改(update),其中删除和插入的代价可以