/**
* 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的更多相关文章

  1. [LeetCode] Edit Distance 编辑距离

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  2. Leetcode:Edit Distance 解题报告

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

  3. [leetcode]Edit Distance @ Python

    原题地址:https://oj.leetcode.com/problems/edit-distance/ 题意: Given two words word1 and word2, find the m ...

  4. [LeetCode] Edit Distance 字符串变换为另一字符串动态规划

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  5. Leetcode Edit Distance

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  6. [LeetCode] Edit Distance(很好的DP)

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  7. LeetCode: Edit Distance && 子序列题集

    Title: Given two words word1 and word2, find the minimum number of steps required to convert word1 t ...

  8. LeetCode——Edit Distance

    Question Given two words word1 and word2, find the minimum number of steps required to convert word1 ...

  9. [LeetCode] One Edit Distance 一个编辑距离

    Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...

  10. 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 ...

随机推荐

  1. Spring Boot实现邮件服务,附常见邮箱的配置

    1. pom.xml文件中引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <art ...

  2. Fiddler功能介绍之Web抓包、远程抓包教程【转载】

    一.fiddler简介 简单来说,Fiddler是一个http协议调试代理工具,它能够记录并检查所有你的电脑和互联网之间的http通讯.网上简介很多,我们不多说. 二.fiddler版本 fiddle ...

  3. 扁平化promise调用链(译)

    这是对Flattened Promise Chains的翻译,水平有限请见谅^ ^. Promises对于解决复杂异步请求与响应问题堪称伟大.AngularJS提供了$q和$http来实现它:还有很多 ...

  4. im4java+GraphicsMagick 的问题

    1.convert.exe: non-conforming drawing primitive definition `' @ error/draw.c/RenderMVGContent/3901. ...

  5. java(一) 基础部分

    1.11.简单讲一下java的跨平台原理 Java通过不同的系统.不同版本.不同位数的java虚拟机(jvm),来屏蔽不同的系统指令集差异而对外体统统一的接口(java API),对于我们普通的jav ...

  6. 起步:Proteus 8 仿真 Arduino 1.8.2

    一.环境准备 1.从Arduino官网或中文社区下载并安装 Arduino IDE 当前最新版1.8.2:http://www.arduino.cn/thread-5838-1-1.html 2.下载 ...

  7. mysql中删除重复记录,只保留一条

    表结构如下: mysql> desc test1; +--------------+------------------+------+-----+---------+------------- ...

  8. 【CF429E】 Points and Segments(欧拉回路)

    传送门 CodeForces 洛谷 Solution 考虑欧拉回路有一个性质. 如果把点抽出来搞成一条直线,路径看成区间覆盖,那么一个点从左往右被覆盖的次数等于从右往左被覆盖的次数. 发现这个性质和本 ...

  9. eclipse中如何自动生成构造函数

    eclipse中如何自动生成构造函数 eclipse是一个非常好的IDE,我在写java程序的时候使用eclipse感觉开发效率很高.而且有很多的快捷和简便方式供大家使用,并且能直接生成class文件 ...

  10. Javascript高级编程学习笔记(34)—— 客户端检测(3)用户代理检测

    用户代理检测 前面的文章介绍的是如何检测浏览器对某一功能的支持情况 但是在实践中我们有些时候免不了需要知道用户到底是用的什么浏览器对我们的站点进行访问 这也是统计用户行为的一部分 用户代理检测这种方式 ...