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

  1. Insert a character
  2. Delete a character
  3. Replace a character

Example 1:

Input: word1 = "horse", word2 = "ros"
Output: 3
Explanation:
horse -> rorse (replace ‘h‘ with ‘r‘)
rorse -> rose (remove ‘r‘)
rose -> ros (remove ‘e‘)把单词1变成单词2,可以修改/删除/插入,最少需要几步

【思路】

动态规划dp[i][j],i为原数据,j为现数据

三种情况plus、del、rep,求min+1(步骤)

【代码】

class Solution {
    public int minDistance(String word1, String word2) {
        int m=word1.length();
        int n=word2.length();
        int cost[][]=new int[m+1][n+1];
        for(int i=0;i<m;i++){
            cost[i][0]=i;}
        for(int j=0;j<n;j++){
            cost[0][j]=j;}

        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(word1.charAt(i)==word2.charAt(j)){
                    cost[i+1][j+1]=cost[i][j];}
                else{
                    int plus=cost[i+1][j];
                    int del=cost[i][j+1];
                    int rep=cost[i][j];
                    cost[i+1][j+1]=Math.min(plus,Math.min(del,rep))+1;
                }
            }
        }
        return cost[m][n];
    }

}

原文地址:https://www.cnblogs.com/inku/p/10035612.html

时间: 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 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

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 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. 编辑距离(DP)

题目 给定两个单词?word1 和?word2,计算出将?word1?转换成?word2 所使用的最少操作数?. 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 示例?1: 输入: word1 = "horse", word2 = "ros" 输出: 3 解释: horse -> rorse (将 'h' 替换为 'r') rorse -> rose (删除 'r') rose -> ros (删除 'e') 来源:力

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(

编辑距离Edit Distance 非常典型的DP类型题目

https://leetcode.com/problems/edit-distance/?tab=Description 真的非常好,也非常典型. https://discuss.leetcode.com/topic/17639/20ms-detailed-explained-c-solutions-o-n-space dp[i][0] = i; dp[0][j] = j; dp[i][j] = dp[i - 1][j - 1], if word1[i - 1] = word2[j - 1];

[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

行编辑距离Edit Distance——动态规划

题目描写叙述: 给定一个源串和目标串.可以对源串进行例如以下操作: 1. 在给定位置上插入一个字符 2. 替换随意字符 3. 删除随意字符 写一个程序.返回最小操作数,使得对源串进行这些操作后等于目标串,源串和目标串的长度都小于2000. 思路: 设状态dp[i][j] 表示从源串s[0...i] 和 目标串t[0...j] 的最短编辑距离 边界为:dp[i][0] = i,dp[0][j] = j 递推方程: 假设s[i] == t[j], 那么 dp[i][j] = dp[i-1][j-1]