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

典型的dp题

class Solution {
public:
    int minDistance(string word1, string word2) {
        int row=word1.size()+1;
        int col=word2.size()+1;
        int isEqual=0;

        int dp[row][col];
        for(int i=0;i<col;++i){
            dp[0][i]=i;
        }
        for(int i=0;i<row;++i){
            dp[i][0]=i;
        }
        for(int i=1;i<row;++i)
            for(int j=1;j<col;++j){
                isEqual=(word1[i-1]==word2[j-1])?0:1;
                dp[i][j]=min(dp[i-1][j]+1,min(dp[i][j-1]+1,dp[i-1][j-1]+isEqual));
            }
        return dp[row-1][col-1];
    }
};
时间: 2024-08-10 02:31:50

Edit Distance(动态规划,难)的相关文章

行编辑距离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]

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

判断两个字符串的edit distance为1

from https://leetcode.com/problems/one-edit-distance/ 正如摘要所说,edit distance是通过变换某些位置的字符使得两个字符串相同的操作数:而该问题是一个比较有名的动态规划问题:分别用s和t表示两个字符串,s(i) 表示s中得第i个字符:用f(i, j)表示s(i)和t(j)的edit distance:那么有以下的递推关系: f(i, j) = min ( f(i - 1, j) + 1; //即删掉s中得第i个字符: f(i, j

[LeetCode] One Edit Distance 一个编辑距离

Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance的拓展,然而这道题并没有那道题难,这道题只让我们判断两个字符串的编辑距离是否为1,那么我们只需分下列三种情况来考虑就行了: 1. 两个字符串的长度之差大于1,那么直接返回False 2. 两个字符串的长度之差等于1,那么长的那个字符串去掉一个字符,剩下的应该和短的字符串相同 3. 两个字符串的长度之

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? 分析:后续遍历

Edit Distance (or Levenshtein Distance) python solution for leetcode EPI 17.2

https://oj.leetcode.com/problems/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

Edit Distance leetcode java

题目: 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) R

Min Edit Distance

Min Edit Distance ----两字符串之间的最小距离 PPT原稿参见Stanford:http://www.stanford.edu/class/cs124/lec/med.pdf Tips:由于本人水平有限,对MED的个人理解可能有纰漏之处,请勿尽信. Edit:个人理解指编辑之意,也即对于两个字符串,对其中的一个进行各种编辑操作(插入.删除.替换)使其变为另一个字符串.要解决的问题是求出最小的编辑操作次数是多少. 基因系列比对 定义距离: X,Y是大小分别为n,m的字符串. 定

最小编辑距离(Minimum edit distance)

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