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

输入两个单词word1和word2,求出从word1转换成word2的最少步骤。每个转换操作算一步。转换操作限定于:

  • 删除一个字符
  • 插入一个字符
  • 替换一个字符

本题用动态规划求解

设决策变量dp[i][j],表示从word[0..i-1]转换为word2[0...j-1]的最少步骤

可以参考http://web.stanford.edu/class/cs124/lec/med.pdf

class Solution {
public:
int minDistance(string word1, string word2) {
int len1 =word1.length(), len2 = word2.length();
if(len1 == ) return len2;
if(len2 == ) return len1;
if(word1 == word2) return ;
vector<vector<int> > dp(len1+, vector<int>(len2+,));
for(int i = ; i <= len1; ++ i) dp[i][] = i;
for(int j = ; j <= len2; ++ j) dp[][j] = j;
for(int i =; i <= len1; ++ i){
for(int j = ; j <= len2; ++ j){
dp[i][j] = min(dp[i-][j-]+(word1[i-] != word2[j-]? : ),min(dp[i-][j]+, dp[i][j-]+));
}
}
return dp[len1][len2];
}
};

Leetcode Edit Distance的更多相关文章

  1. [LeetCode] Edit Distance 编辑距离

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  2. Leetcode:Edit Distance 解题报告

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

  3. [leetcode]Edit Distance @ Python

    原题地址:https://oj.leetcode.com/problems/edit-distance/ 题意: Given two words word1 and word2, find the m ...

  4. [LeetCode] Edit Distance 字符串变换为另一字符串动态规划

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  5. [LeetCode] Edit Distance(很好的DP)

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  6. LeetCode: Edit Distance && 子序列题集

    Title: Given two words word1 and word2, find the minimum number of steps required to convert word1 t ...

  7. LeetCode——Edit Distance

    Question Given two words word1 and word2, find the minimum number of steps required to convert word1 ...

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

    Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...

  9. Java for LeetCode 072 Edit Distance【HARD】

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

随机推荐

  1. 浅谈Android中拍照、从相册选择图片并截图相关知识点

    前言 我们在Android开发中经常会需要使用相机或者从相册中选取图片的情况,今天就把这里面相关的知识点总结下,方便以后开发的时候使用. 1.相机拍照并可自定义截图功能 我们先来看如何使用Intent ...

  2. [Fluent NHibernate]一对多关系处理

    目录 写在前面 系列文章 一对多关系 总结 写在前面 上篇文章简单介绍了,Fluent Nhibernate使用代码的方式生成Nhibernate的配置文件,以及如何生成持久化类的映射文件.通过上篇的 ...

  3. 如何让ConfigurationManager打开任意的配置文件

    VisualStudio的配置文件很好很强大,用来保存数据库连接字符串或键值对都非常方便,只需要通过ConfigurationManager的ConnectionStrings或AppSettings ...

  4. 如何删除PHP数组中的元素,并且索引重排(unset,array_splice)?

    如果要在某个数组中删除一个元素,可以直接用的unset,但是数组的索引不会重排: <?php $arr = array('a','b','c','d'); unset($arr[1]); pri ...

  5. Linux启动过程详解(inittab、rc.sysinit、rcX.d、rc.local)

    启动第一步--加载BIOS 当你打开计算机电源,计算机会首先加载BIOS信息,BIOS信息是如此的重要,以至于计算机必须在最开始就找到它.这是因为BIOS中包含了CPU的相关信息.设备启动顺序信息.硬 ...

  6. C和指针 第六章 数组名与指针

    指针的算术运算符是指针和数组之间的一种关联,但不是唯一关联: 可以使用数组名作为指向数组第一个元素的指针,但是不可以给数组名赋新的值. //如下声明a int a[10]; //用a作为指向数组第一个 ...

  7. DAY5 python内置函数+验证码实例

    内置函数 用验证码作为实例 字符串和字节的转换 字符串到字节 字节到字符串

  8. TouchSlide1.1,手机上的幻灯片

    TouchSlide 是纯javascript打造的触屏滑动特效插件 http://pan.baidu.com/s/1bpoWNin 官网:http://www.superslide2.com/Tou ...

  9. MongoDB_C_Driver使用教程(2)高级连接

    高级连接(Advanced Connection) 以下指南包含MongoDB配置的特定类型的信息. 简单的连接到独立服务器的示例,请参考MongoDB_C_Dirver使用教程. 要连接到启用身份验 ...

  10. hmm

    http://www.cnblogs.com/mindpuzzle/p/3653043.html http://en.wikipedia.org/wiki/Viterbi_algorithm http ...