Edit Distance leetcode java
题目:
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
题解:
处理这道题也是用动态规划。
动态数组dp[word1.length+1][word2.length+1]
dp[i][j]表示从word1前i个字符转换到word2前j个字符最少的步骤数。
假设word1现在遍历到字符x,word2遍历到字符y(word1当前遍历到的长度为i,word2为j)。
以下两种可能性:
1. x==y,那么不用做任何编辑操作,所以dp[i][j] = dp[i-1][j-1]
2. x != y
(1) 在word1插入y, 那么dp[i][j] = dp[i][j-1] + 1
(2) 在word1删除x, 那么dp[i][j] = dp[i-1][j] + 1
(3) 把word1中的x用y来替换,那么dp[i][j] = dp[i-1][j-1] + 1
最少的步骤就是取这三个中的最小值。
最后返回 dp[word1.length+1][word2.length+1] 即可。
代码如下:
1 public static int minDistance(String word1, String word2) {
2 int len1 = word1.length();
3 int len2 = word2.length();
4
5 // len1+1, len2+1, because finally return dp[len1][len2]
6 int[][] dp = new int[len1 + 1][len2 + 1];
7
8 for (int i = 0; i <= len1; i++)
9 dp[i][0] = i;
for (int j = 0; j <= len2; j++)
dp[0][j] = j;
//iterate though, and check last char
for (int i = 1; i <= len1; i++) {
char c1 = word1.charAt(i-1);
for (int j = 1; j <= len2; j++) {
char c2 = word2.charAt(j-1);
//if last two chars equal
if (c1 == c2) {
//update dp value for +1 length
dp[i][j] = dp[i-1][j-1];
} else {
int replace = dp[i-1][j-1] + 1;
int insert = dp[i-1][j] + 1;
int delete = dp[i][j-1] + 1;
int min = Math.min(replace, insert);
min = Math.min(min,delete);
dp[i][j] = min;
}
}
}
return dp[len1][len2];
}
Reference:
http://www.programcreek.com/2013/12/edit-distance-in-java/
http://blog.csdn.net/linhuanmars/article/details/24213795
Edit Distance leetcode java的更多相关文章
- edit distance leetcode
这样的字符转换的dp挺经典的, 若word1[i+1]==word2[j+1] dp[i+1][j+1] = dp[i][j]:否则,dp[i+1][j+1] = dp[i][j] + 1.(替换原则 ...
- 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 ...
- ✡ leetcode 161. One Edit Distance 判断两个字符串是否是一步变换 --------- java
Given two strings S and T, determine if they are both one edit distance apart. 给定两个字符串,判断他们是否是一步变换得到 ...
- LeetCode One Edit Distance
原题链接在这里:https://leetcode.com/problems/one-edit-distance/ Given two strings S and T, determine if the ...
- 【LeetCode】161. One Edit Distance
Difficulty: Medium More:[目录]LeetCode Java实现 Description Given two strings S and T, determine if the ...
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- [leetcode]161. One Edit Distance编辑步数为一
Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 pos ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance
引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...
随机推荐
- linux 驱动之LCD驱动(有framebuffer)
<简介> LCD驱动里有个很重要的概念叫帧缓冲(framebuffer),它是Linux系统为显示设备提供的一个接口,应用程序在图形模式允许对显示缓冲区进行读写操作.用户根本不用关心物理显 ...
- bzoj 4004 向量拟阵
题解RT. eps = 1e-10 WrongAnswer eps = 1e-5 Accepted /************************************************* ...
- 如何利用JS判断当前来路域名并跳转到指定页面
1.如何利用JS判断当前来路域名并跳转到指定页面 获取当前请求路径var href = location.href ;if(href.indexOf("baidu")>-1) ...
- consul vs etcd3
https://sysadmin.libhunt.com/project/etcd/vs/consul
- STM32 USART 波特率计算
The baud rate for the receiver and transmitter (Rx and Tx) are both set to the same value as program ...
- 【Go命令教程】14. go env
命令 go env 用于打印 Go 语言的环境信息.其中的一些信息我们在之前已经多次提及,但是却没有进行详细的说明.在本小节,我们会对这些信息进行深入介绍.我们先来看一看 go env 命令情况下都会 ...
- socket shutdown和close的区别
http://www.jianshu.com/p/eecab8d50697 shutdown() doesn't actually close the file descriptor—it just ...
- 没有可用的复制构造函数或复制构造函数声明为“explicit”
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\vector(810) : error C2558: str ...
- 使用jQuery实现input数值的增量和减量
在很多电商网站中,在购物车所在页面,涉及到商品数量的时候,都会提供一个+号按钮和-号按钮来实现增1和减1,并且只允许input中输入数值.Bootstrap TouchSpin这款插件就是针对此需求而 ...
- Android 数据存储02之文件读写
Android文件读写 版本 修改内容 日期 修改人 V1.0 原始版本 2013/2/25 skywang Android文件读写的有两种方式.一种,是通过标准的JavaIO库去读写.另一种,是通过 ...