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 ...
随机推荐
- MySQL Crash Course #17# Chapter 25. 触发器(Trigger)
推荐看这篇mysql 利用触发器(Trigger)让代码更简单 以及 23.3.1 Trigger Syntax and Examples 感觉有点像 Spring 里的 AOP 我们为什么需要触发器 ...
- 来了解一下Ajax是什么?Ajax的原理?Ajax与传统Web比较?Ajax的优缺点?Ajax的Post与Get比较
一.什么是Ajax Ajax(Asynchronous Java and XML的缩写)是一种异步请求数据的web开发技术,对于改善用户的体验和页面性能很有帮助.简单地说,在不需要重新刷新页面的情况下 ...
- 04:获取zabbix监控信息
目录:Django其他篇 01: 安装zabbix server 02:zabbix-agent安装配置 及 web界面管理 03: zabbix API接口 对 主机.主机组.模板.应用集.监控项. ...
- PHP-Iterator迭代器(遍历)接口详讲
echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> "; class ...
- SQL语句 查询同一个字符在某一个字符串中出现的次数
select len(replace(字段名A,';','--'))-len(字段名A) from table表名
- Django框架(四) Django之视图层
视图函数 一个视图函数,简称视图,是一个简单的Python 函数,它接受Web请求并且返回Web响应.响应可以是一张网页的HTML内容,一个重定向,一个404错误,一个XML文档,或者一张图片. . ...
- NYOJ 16 矩形嵌套(经典DP)
http://acm.nyist.net/JudgeOnline/problem.php?pid=16 矩形嵌套 时间限制:3000 ms | 内存限制:65535 KB 难度: ...
- python 获取文件的修改时间
os.path.getmtime(name) #获取文件的修改时间 os.stat(path).st_mtime#获取文件的修改时间 os.stat(path).st_ctime #获取文件修改时间 ...
- faster-rcnn 论文讲解
Faster RCN已经将特征抽取(feature extraction),proposal提取,bounding box regression(rect refine),classification ...
- git连接华为开发云devcloud
华为开发运在代码托管方面的个github很类似,引入了代码仓库的概念,同时需要本地安装git客户端,且只能与git进行连接,从这个角度上讲,华为开发云的代码管理部分就是github的功能,下面对git ...