leetcode — edit-distance
/**
* Source : https://oj.leetcode.com/problems/edit-distance/
*
*
* 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
*/
public class EditDistance {
/**
* 计算出从一个单词变到另一个单词的最少步数,也就是最短距离,只能使用插入、删除、替换操作
*
* 考虑两个单词abc,bbcd,dp[i][j]表示word1的前i个字符变到word2的前j个字符,所需要的步数,""表示空串
* "" a b c
* "" 0 1 2 3
* b 1 1 1 2
* b 1 1 1 2
* c 3 3 2 1
* d 4 4 3 2
*
* 从上面的演算可以看出
* 当word1[i] == word2[j]的时候,dp[i][j] = dp[i-1][j-1]
* 当word1[i] != word2[j]的时候,dp[i][j] = 其左边、左上方、正上方三个数字中最小的那一个加1
*
*
* @param word1
* @param word2
* @return
*/
public int minimumDistance (String word1, String word2) {
if (word1.length() == 0) {
return word2.length();
}
if (word2.length() == 0) {
return word1.length();
}
int[][] dp = new int[word1.length() + 1][word2.length() + 1];
for (int i = 0; i <= word1.length(); i++) {
dp[i][0] = i;
}
for (int i = 0; i <= word2.length(); i++) {
dp[0][i] = i;
}
for (int i = 1; i <= word1.length(); i++) {
for (int j = 1; j <= word2.length(); j++) {
if (word1.charAt(i-1) == word2.charAt(j-1)) {
dp[i][j] = dp[i-1][j-1];
} else {
dp[i][j] = Math.min(Math.min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]) + 1;
}
}
}
return dp[word1.length()][word2.length()];
}
public static void main(String[] args) {
EditDistance editDistance = new EditDistance();
System.out.println(editDistance.minimumDistance("", "abc"));
System.out.println(editDistance.minimumDistance("b", "abc"));
System.out.println(editDistance.minimumDistance("bb", "abc"));
System.out.println(editDistance.minimumDistance("bbc", "abc"));
System.out.println(editDistance.minimumDistance("bbcd", "abc"));
}
}
leetcode — edit-distance的更多相关文章
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- Leetcode:Edit Distance 解题报告
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- [leetcode]Edit Distance @ Python
原题地址:https://oj.leetcode.com/problems/edit-distance/ 题意: Given two words word1 and word2, find the m ...
- [LeetCode] Edit Distance 字符串变换为另一字符串动态规划
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- Leetcode Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- [LeetCode] Edit Distance(很好的DP)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- LeetCode: Edit Distance && 子序列题集
Title: Given two words word1 and word2, find the minimum number of steps required to convert word1 t ...
- LeetCode——Edit Distance
Question Given two words word1 and word2, find the minimum number of steps required to convert word1 ...
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- 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 ...
随机推荐
- # 2019-2020-4 《Java 程序设计》第六周总结
2019-2020-4 <Java 程序设计>第六周知识总结 第七章:内部类与异常类 1.内部类 (1)类可以有两种重要的成员:成员变量和方法,类还可以有一种成员:内部类. (2)java ...
- 2019.03.09 codeforces620E. New Year Tree(线段树+状态压缩)
传送门 题意:给一棵带颜色的树,可以给子树染色或者问子树里有几种不同的颜色,颜色值不超过606060. 思路:颜色值很小,因此状压一个区间里的颜色用线段树取并集即可. 代码: #include< ...
- Day04 (黑客成长日记) 集合记录
集合 集合:是可变的数据类型 ,它里面的数据类型必须是不可变的数据类型,无序,不重复,不同于字典,他有元素,没有键值对(编码不常用) li = set([1,2,3]) li = {'alex','w ...
- IMDb、烂番茄、MTC、各种电影行业评分名字整理
这篇不是技术文章,就是对总是看到但是不知道具体是什么的一些电影名词.评分.来源,学习一下. IMDb 互联网电影资料库(Internet Movie Database,简称IMDb)是一个关于电影演员 ...
- UEditor可以如何直接复制word的图文内容到编辑器中?
下载并打开工程: 文档的上传 运行: 复制随便一篇文档,粘贴进去. 通过粘贴后,文档以及图片被粘贴进来了,看看html代码: 图片全部使用img标签统一.传输进度条的效果也不错. 文档图片被放置在 ...
- docker install
1.安装必要工具集 sudo yum install -y yum-utils 2.安装Docker官方源 sudo yum-config-manager \ --add-repo \ https:/ ...
- ppt制作动态表格与文字
在工作中经常与ppt打交道的小伙伴们,是不是非常想让自己做的ppt图表能够动起来,显得高大上一点呢?比如让柱形图慢慢长起来,让折线图慢慢画出来,让文字像字幕一样缓缓上升?本文将给大家整理几 ...
- php中ob_get_contents、curl_multi_init、curl_init多线程下载远程图片并保存记录
php中三种方式测试图片下载效率 原文共24张不同图,每张大小在500K以上 使用时注意调整传入数组格式以及需要下载时保存地址的路径格式等 这三种方式无需额外安装扩展,方便快捷易操作[虽然效率看结果没 ...
- Python之路【第四篇】Python基础2
一.格式化输出 按要求输出信息 name=input("name:") age=int(input("age:")) job=input("job:& ...
- PMP:10.项目采购管理
内容中包含 base64string 图片造成字符过多,拒绝显示