题目描述

地址 : https://leetcode.com/problems/edit-distance/description/

思路

  • 使用dp[i][j]用来表示word10~i-1word20~j-1的最小编辑距离
  • 我们可以知道边界情况:dp[i][0] = idp[0][j] = j,代表从 "" 变为 dp[0~i-1]dp[0][0~j-1] 所需要的次数

同时对于两个字符串的子串,都能分为最后一个字符相等或者不等的情况:

  • 如果word1[i-1] == word2[j-1]dp[i][j] = dp[i-1][j-1]
  • 如果word1[i-1] != word2[j-1]
    • 向word1插入:dp[i][j] = dp[i][j-1] + 1
    • 从word1删除:dp[i][j] = dp[i-1][j] + 1
    • 替换word1元素:dp[i][j] = dp[i-1][j-1] + 1
 public int minDistance(String word1, String word2) {
int n = word1.length();
int m = word2.length();
int[][] dp = new int[n + 1][m + 1];
for (int i = 0; i < m + 1; i++) {
dp[0][i] = i;
}
for (int i = 0; i < n + 1; i++) {
dp[i][0] = i;
}
for (int i = 1; i < n + 1; i++) {
for (int j = 1; j < m + 1; 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[n][m];
}

Java解决LeetCode72题 Edit Distance的更多相关文章

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

  2. [Swift]LeetCode72. 编辑距离 | Edit Distance

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

  3. [UCSD白板题] Compute the Edit Distance Between Two Strings

    Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...

  4. ✡ leetcode 161. One Edit Distance 判断两个字符串是否是一步变换 --------- java

    Given two strings S and T, determine if they are both one edit distance apart. 给定两个字符串,判断他们是否是一步变换得到 ...

  5. 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...

  6. 刷题72. Edit Distance

    一.题目说明 题目72. Edit Distance,计算将word1转换为word2最少需要的操作.操作包含:插入一个字符,删除一个字符,替换一个字符.本题难度为Hard! 二.我的解答 这个题目一 ...

  7. 72. Edit Distance

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

  8. LeetCode One Edit Distance

    原题链接在这里:https://leetcode.com/problems/one-edit-distance/ Given two strings S and T, determine if the ...

  9. Minimum edit distance(levenshtein distance)(最小编辑距离)初探

    最小编辑距离的定义:编辑距离(Edit Distance),又称Levenshtein距离.是指两个字串之间,由一个转成还有一个所需的最少编辑操作次数.许可的编辑操作包含将一个字符替换成还有一个字符. ...

随机推荐

  1. Gradle入门(1):安装

    在Ubuntu下,执行以下命令: sudo apt-get install gradle 安装完成后,执行命令: gradle -v 得到以下信息: Picked up _JAVA_OPTIONS:  ...

  2. 6/8 sprint2 看板和燃尽图的更新

  3. TADOConnection.Close - connection still active on MS-SQL server

    I have several Delphi programs (XE3), that use a TADOConnection to connect to a MS-SQL Server. I rec ...

  4. 用SQL查询方式显示GROUP BY中的TOP解决方法[转]

    用SQL查询方式显示GROUP BY中的TOP怎样用一个SQL语句来显示 分组后每个组的前几位 比如把一个学校所有学生的成绩按班级分组,再显示每个班级前五名的信息. 班级     学生   成绩 一班 ...

  5. OpenSSL 自签名证书

    通过下面9步,可以轻松生成自签名证书. 1.安装.部署OpenSSL 略 2.创建文件夹(下面通常root文件夹).用来放即将创建的各种证书等.如:I:\Key10.167.219.64 3.在roo ...

  6. Java并发编程:线程池

    一.为什么使用线程池 使用线程的时候直接就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了,这样频繁创建线程就会大大降 ...

  7. A Chess Game HDU - 1524 (有向图博弈)

    题意:在一个有向无环图上有n个顶点,每一个顶点都只有一个棋子,有两个人,每次根据这个图只能将任意一颗棋子移动一步 ,如果到某一步玩家不能移动时,那么这个人就输. 分析:本题是最典型的有向无环图的博弈, ...

  8. jQuery map和each用法

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. LaTex Font Size 字体大小

    目录 命令 效果图 命令 LaTex中字体大小由以下命令控制: \tiny \scriptsize \footnotesize \small \normalsize \large \Large \LA ...

  10. JMH 使用指南 - java 性能测试

    JMH 篇 JMH,即Java Microbenchmark Harness 翻译:java 微基准测试 工具套件.## 1.添加依赖```<dependency> <groupId ...