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. HDOJ 1418 抱歉(欧拉公式)

    Problem Description 非常抱歉,本来兴冲冲地搞一场练习赛,由于我准备不足,出现很多数据的错误,现在这里换一个简单的题目: 前几天在网上查找ACM资料的时候,看到一个中学的奥数题目,就 ...

  2. cf498C Array and Operations

    C. Array and Operations time limit per test 1 second memory limit per test 256 megabytes input stand ...

  3. 转自http://blog.sina.com.cn/daylive——C++ STL set&multiset

    C++ STL set和multiset的使用 1,set的含义是集合,它是一个有序的容器,里面的元素都是排序好的,支持插入,删除,查找等操作,就  像一个集合一样.所有的操作的都是严格在logn时间 ...

  4. AES的S-BOX构造

    利用GF(2^8)乘法逆元,以及矩阵运算,可以构造出AES的SBOX. 求乘法逆元的一般方法是利用扩展欧几里得定理,在这里我取了个巧. 因为我已经有了GF的指数表(见上一篇文),利用指数表可以轻易地构 ...

  5. Spark standalone安装(最小化集群部署)

    Spark standalone安装-最小化集群部署(Spark官方建议使用Standalone模式)        集群规划:    主机        IP                    ...

  6. Hadoop2.4.1 使用MapReduce简单的数据清洗

    package com.bank.service; import java.io.IOException;import java.text.ParseException;import java.tex ...

  7. Sqlserver更新数据表xml类型字段内容某个节点值的脚本

    GO USE [JC2010_MAIN_DB] 1.新建备份表JobObjectVersion_JCSchemVersion_BCK) GO IF EXISTS (SELECT * FROM sys. ...

  8. [置顶] ARM指令集和常用寄存器

    1) ARM指令集 32位的 ARM指令和 16位 的Thumb指令 1,寄存器寻址 MOV R1, R2  //将寄存器R2的值传给寄存器R1 2,立即寻址 MOV R0, #0XFF00 //数据 ...

  9. mysql主从监控

    要求:检测myslq从库状态,跳过固定的错误号,每隔30秒检测一次,如果符合条件自动跳过或者是重启从库 1)取出mysql从库的关键字 [root@localhost scripts]# mysql ...

  10. OD: Universal Shellcode

    本节讲如果开发通用的 Shellcode. Shellcode 的组织 shellcode 的组织对成功地 exploit 很重要. 送入缓冲区的数据包括: . 填充物.一般用 0x90 (NOP) ...