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

  

class Solution {
public:
int minDistance(string word1, string word2) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int m = word1.size();
int n = word2.size();
vector<vector<int>> table(m+1,vector<int>(n+1, 0)); for(int i = 1; i <= n; i++) table[0][i] = i;
for(int i = 1; i <= m; i++) table[i][0] = i;
for(int i = 1; i <= m; i++)
for(int j = 1; j <= n;j++)
{
int min1 = table[i-1][j] < table[i][j-1] ? table[i-1][j] : table[i][j-1];
min1++;
int min2 = word1[i-1] == word2[j-1] ? 0 : 1;
min2 += table[i-1][j-1];
table[i][j] = min1 < min2 ? min1 : min2;
} return table[m][n];
}
};

  reference :http://www.geeksforgeeks.org/dynamic-programming-set-5-edit-distance/

LeetCode_Edit Distance的更多相关文章

  1. [LeetCode] Total Hamming Distance 全部汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  2. [LeetCode] Hamming Distance 汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  3. [LeetCode] Rearrange String k Distance Apart 按距离为k隔离重排字符串

    Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...

  4. [LeetCode] Shortest Distance from All Buildings 建筑物的最短距离

    You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...

  5. [LeetCode] Shortest Word Distance III 最短单词距离之三

    This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...

  6. [LeetCode] Shortest Word Distance II 最短单词距离之二

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

  7. [LeetCode] Shortest Word Distance 最短单词距离

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

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

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

  9. [LeetCode] Edit Distance 编辑距离

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

随机推荐

  1. JVM内存堆布局图解分析

    JAVA能够实现跨平台的一个根本原因,是定义了class文件的格式标准,凡是实现该标准的JVM都能够加载并解释该class文件,据此也可以知道,为啥Java语言的执行速度比C/C++语言执行的速度要慢 ...

  2. 关于数据表命名为mysql保留的时候的操作

    今天操作数据表的时候,发现order数据表无法进行操作,必须加上反单引号才能进行操作,查了一下原因: 反引号是用来区别mysql关键字的,比如,如果你有一个表名叫select,你就必须写成`selec ...

  3. javascript 单个图片的淡入淡出效果和多张图片的淡入淡出效果

    最近刚好在看之前妙趣网站的javascript 初级运动教程,教程里说设置图片的透明度使用了一个变量.这种方法确实不错,但是燕姐喜欢麻烦.就用自己的理解方法写了一遍.其中也是各种坑.现在先把一个图片的 ...

  4. Linux下那些奇葩的命令

    相信喜爱编程,痴迷技术的你,肯定接触过甚至深爱着linux,甚至可能已经很熟悉linux了,可是linux那逗比的一面,你又知道多少. 本文!纯粹娱乐!不喜勿喷! 1.程序猿的愤慨! yes 当我们再 ...

  5. Linux下Redis的安装、配置操作说明

    Redis 是一个高性能的key-value数据库. redis的出现,很大程度补偿了memcached这类keyvalue存储的不足,在部分场合可以对关系数据库起到很好的补充作用.它提供了Pytho ...

  6. android requestDisallowInterceptTouchEvent用途

    ViewPager来实现左右滑动切换tab.假设tab的某一项中嵌入了水平可滑动的View就会让你有些不爽,比方想滑动tab项中的可水平滑动的控件,却导致tab切换. 由于Android事件机制是从父 ...

  7. 如何使用easyUI

    一.简介 以下内容来自百度: jQuery EasyUI是一组基于jQuery的UI插件集合,而jQuery EasyUI的 目标就是帮助web开发者更轻松的打造出功能丰富并且美观的UI界面.开发者不 ...

  8. [小技巧][ASP.Net MVC Hack] 使用 HTTP 报文中的 Header 字段进行身份验证

    在一些 Web 系统中,身份验证是依靠硬件证书进行的:在电脑上插入 USB 证书,浏览器插件读取证书的相关信息,然后在发送 HTTP 登录请求时顺便在 Header 字段附加上身份信息.服务器端处理这 ...

  9. 笔记本分享无线Wifi

    两种方法: 一.使用软件分享,如Wifi共享精灵,设置非常简单. 二.开启windows 7的隐藏功能:虚拟WiFi和SoftAP(即虚拟无线AP),就可以让电脑变成无线路由器,实现共享上网,节省网费 ...

  10. DataGrid( 数据表格) 组件[5]

    本节课重点了解 EasyUI 中 DataGrid(数据表格)组件的使用方法,这个组件依赖于Panel(面板).Resizeable(调整大小).LinkButton(按钮).Pageination( ...