题目:

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

  1. 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.(替换原则 ...

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

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

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

  4. LeetCode One Edit Distance

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

  5. 【LeetCode】161. One Edit Distance

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given two strings S and T, determine if the ...

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

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

  7. [LeetCode] Edit Distance 编辑距离

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

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

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

随机推荐

  1. 面向对象设计原则 单一职责原则(Single responsibility principle)

    单一职责原则(SRP:Single responsibility principle) 又称单一功能原则,面向对象的基本原则之一.它规定 一个类应该只有一个发生变化的原因. 该原则由罗伯特·C·马丁( ...

  2. [USACO07JAN]Balanced Lineup

    OJ题号:洛谷2880 思路1: 线段树维护区间最大最小值. #include<cstdio> #include<cctype> #include<utility> ...

  3. Vakuum开发笔记01 开天辟地

    1.缘起 先驱--COGS 早在2008年,我自学PHP后开发了COGS,并成功用于学校内部的OJ,ruvtex.也曾经对外开放过,但是由于学校网络不稳定,后来一直连不上了.我还把COGS推荐给了OO ...

  4. Springboot 线程池配置

    最近的项目里要手动维护线程池,然后看到一起开发的小伙伴直接用Java了,我坚信Springboot不可能没这功能,于是查了些资料,果然有,这里给一下. 首先我们都知道@Async标签能让方法异步执行, ...

  5. Docker系列之(一):10分钟玩转Docker

    1.前言 进入云计算的时代,各大云提供商AWS,阿里云纷纷推出针对Docker的服务,现在Docker是十分火爆,那么Docker到底是什麽,让我们来体验一下. 2.Docker是什麽 Docker是 ...

  6. centos 7下安装MySQL5.7 的安装和配置

    原文链接:  http://blog.csdn.net/xyang81/article/details/51759200 安装环境:CentOS7 64位 MINI版,安装MySQL5.7 1.配置Y ...

  7. 使用 IntraWeb (21) - 基本控件之 TIWTabControl

    TIWTabControl 包含的是 TIWTabPage; 设计时通过右键菜单 Add Page 添加(再给页面添加东西时一定要先选定页面); 下面例子是动态添加的. TIWTabControl 所 ...

  8. LPC43xx SGPIO Slice 输入输出连接表

  9. Open JTAG Project

    Open JTAG Project is an open source hardware and software for a high speed USB JTAG tool. In this si ...

  10. 采用ASP.NET IIS 注册工具 (Aspnet_regiis.exe)对web.config实行本地加密

    加密原因:我们通常将一些重要的配置信息写在Web.config里面,其中数据库链接就是这样的信息.将这些数据直接明文显示,显然不太安全. 工具: 采用ASP.NET IIS 注册工具 (Aspnet_ ...