72. Edit Distance *HARD*
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
* Dynamic Programming
* Definitaion
* m[i][j] is minimal distance from word1[0..i] to word2[0..j]
* So,
* 1) if word1[i] == word2[j], then m[i][j] == m[i-1][j-1].
* 2) if word1[i] != word2[j], then we need to find which one below is minimal:
* min( m[i-1][j-1], m[i-1][j], m[i][j-1] ) and +1 - current char need be changed.
* Let's take a look m[1][2] : "a" => "ab"
* +---+ +---+
* ''=> a | 1 | | 2 | '' => ab
* +---+ +---+
* +---+ +---+
* a => a | 0 | | 1 | a => ab
* +---+ +---+
*
* To know the minimal distance `a => ab`, we can get it from one of the following cases:
* 1) delete the last char in word1, minDistance( '' => ab ) + 1
* 2) delete the last char in word2, minDistance( a => a ) + 1
* 3) change the last char, minDistance( '' => a ) + 1
* For Example:
* word1="abb", word2="abccb"
* 1) Initialize the DP matrix as below:
* "" a b c c b
* "" 0 1 2 3 4 5
* a 1
* b 2
* b 3
* 2) Dynamic Programming
* "" a b c c b
* "" 0 1 2 3 4 5
* a 1 0 1 2 3 4
* b 2 1 0 1 2 3
* b 3 2 1 1 1 2
int min(int x, int y, int z) {
return std::min(x, std::min(y,z));
} int minDistance(string word1, string word2) {
int n1 = word1.size();
int n2 = word2.size();
if (n1==) return n2;
if (n2==) return n1;
vector< vector<int> > m(n1+, vector<int>(n2+));
for(int i=; i<m.size(); i++){
m[i][] = i;
}
for (int i=; i<m[].size(); i++) {
m[][i]=i;
} //Dynamic Programming
int row, col;
for (row=; row<m.size(); row++) {
for(col=; col<m[row].size(); col++){
if (word1[row-] == word2[col-] ){
m[row][col] = m[row-][col-];
}else{
int minValue = min(m[row-][col-], m[row-][col], m[row][col-]);
m[row][col] = minValue + ;
}
}
} return m[row-][col-];
}
72. Edit Distance *HARD*的更多相关文章
- 【Leetcode】72 Edit Distance
72. Edit Distance Given two words word1 and word2, find the minimum number of steps required to conv ...
- 刷题72. Edit Distance
一.题目说明 题目72. Edit Distance,计算将word1转换为word2最少需要的操作.操作包含:插入一个字符,删除一个字符,替换一个字符.本题难度为Hard! 二.我的解答 这个题目一 ...
- [LeetCode] 72. Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- 72. Edit Distance
题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to w ...
- leetCode 72.Edit Distance (编辑距离) 解题思路和方法
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- [LeetCode] 72. Edit Distance(最短编辑距离)
传送门 Description Given two words word1 and word2, find the minimum number of steps required to conver ...
- LeetCode - 72. Edit Distance
最小编辑距离,动态规划经典题. Given two words word1 and word2, find the minimum number of steps required to conver ...
- 72. Edit Distance(困难,确实挺难的,但很经典,双序列DP问题)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- 【一天一道LeetCode】#72. Edit Distance
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
随机推荐
- PHP二维数组排序(感谢滔哥lvtao.net)
滔哥原创 /* _ooOoo_ o8888888o 88" . "88 (| -_- |) O\ = /O ____/`---'\____ .' \\| |// `. / \\|| ...
- 10:Python2与Python3比较
1.print 函数 1. print语句没有了,取而代之的是print()函数. Python 2.6与Python 2.7部分地支持这种形式的print语法. 2.Unicode 1. 在pyt ...
- 20145337 《网络对抗技术》Web基础
20145337 <网络对抗技术>Web基础 一.实验后回答问题 什么是表单? 表单是HTML的一个重要部分,主要用于将用户输入的信息提交到服务器.如果是普通的HTML页面,则当浏览器提出 ...
- FATFS(A)
(一),什么是文件管理系统 答:数据在PC上是以文件的形式储存在磁盘中的,这些数据的形式一般为ASCII码或二进制形式.简单点说就是:管理磁盘上的文件的方法的代码! 如:我们写到SD卡上面的数据管理一 ...
- 把一个activity作为弹窗
1.可以在这个activity的xml中设置其高度为某个固定高度 2.在java中:getWindow().setGravity(Gravity.BOTTOM);//设置在底部出现 getWindo ...
- 高通计划停用MSM 以SDM为移动平台命名【转】
本文转载自:http://www.eeworld.com.cn/xfdz/article_2017061566458.html 据外媒报道,高通公司计划停用骁龙移动平台型号前面的MSM字样,以SDM取 ...
- 从bios启动说起
如果从bios启动说起的话,BIOS去加载bootloader,bootloader去加载操作系统,那么bootloader是怎么找到操作系统的呢?经过一些流程后,它会去找grub:然后通过grub提 ...
- 【集群搭建】Zookeeper集群环境配置
1.下载解压安装文件 2.配置文件:conf/zoo.cfg tickTime=2000 dataDir=/usr/sunny/logs/zookeeper/data dataLogDir=/usr/ ...
- DBMS 数据库管理系统 DataBase Management System
- SQL 事务(Transaction)
1.概念 指访问并可能更新数据库中各种数据项的一个程序执行单元(unit)由多个sql语句组成,必须作为一个整体执行这些sql语句作为一个整体一起向系统提交,要么都执行.要么都不执行 语法步骤:开始事 ...