博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode#72]Edit Distance
阅读量:5365 次
发布时间:2019-06-15

本文共 3450 字,大约阅读时间需要 11 分钟。

Problem:

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

Analysis:

This problem is very ticky and easy!!!Reference: https://en.wikipedia.org/wiki/Edit_distanceFor String problem, if we were asked to compare to string according to order, this problem could and should always be efficiently sovled by dynamic programming method. If the problem allow the string to be permutated, we should take care, since it usually should be solved through HashMap way.Idea:Assume you have a dist array, dist[i+1][j+1] means the distance between word1[0, i] and word2[0, j]. You should try to use the proper transitional function to reach the answer dist[word1.length][word2.length].Note: for this problem dist[0][0] is for empty string "" "". Transitional functions:1. iff word1[i] == word2[j], dist[i+1][j+1] = dist[i][j].if (word1.charAt(i-1) == word2.charAt(j-1)) {    dist[i][j] = dist[i-1][j-1];}2. iff word1[i] != word2[j], the following ways could be used:    2.1 subsitute word1[i] with the same character of word2[j], dist[i+1][j+1] = dist[i][j] + 1.    2.2 add word1[i] into word2, dist[i+1][j+1] = dist[i][j+1] + 1(word1[i] == added)    2.3 add word1[j] into word2, the same as above.     if (word1.charAt(i-1) == word2.charAt(j-1)) {    ...} else{    int sub = dist[i-1][j-1] + 1;    int add_a = dist[i-1][j] + 1;    int add_b = dist[i][j-1] + 1;    dist[i][j] = Math.min(sub, Math.min(add_a, add_b));}Skills:1. Why don't we consider the method of delete?Delete a character from one string is equal to add a character into one string, both of them need one extra operation. To add a character into a string, To delete a word from word2, dist[i][j] = dist[i][j-1] + 1;Thus we have no need to consider the case of deleting a character. 2. Since the transitional function use dist[i-1][j-1], dist[i-1][j] and dist[i][j-1], to avoid corner cases, we should take advatage of empty string. (which help to keep invariance right and code simple!). But you should take care the change in indexing. the i is equeal to i-1 in word's indexing system.int[][] dist = new int[m+1][n+1];for (int i = 0; i <= m; i++)    dist[i][0] = i;for (int j = 0; j <= n; j++)    dist[0][j] = j;0    | 1 2 3 . . . ---  |------------ 1   | 2   | 3   | .   | .   |Key: you must initate the right value for additional row and column.

Solution:

public class Solution {    public int minDistance(String word1, String word2) {        if (word1 == null || word2 == null)            return 0;        int m = word1.length();        int n = word2.length();        int[][] dist = new int[m+1][n+1];        for (int i = 0; i <= m; i++)            dist[i][0] = i;        for (int j = 0; j <= n; j++)            dist[0][j] = j;        for (int i = 1; i <= m; i++) {            for (int j = 1; j <= n; j++) {                if (word1.charAt(i-1) == word2.charAt(j-1)) {                    dist[i][j] = dist[i-1][j-1];                } else{                    int sub = dist[i-1][j-1] + 1;                    int add_a = dist[i-1][j] + 1;                    int add_b = dist[i][j-1] + 1;                    dist[i][j] = Math.min(sub, Math.min(add_a, add_b));                }            }        }        return dist[m][n];    }}

 

转载于:https://www.cnblogs.com/airwindow/p/4762951.html

你可能感兴趣的文章
十进制与十六进制的相互转换
查看>>
在Flex中用Validator检测数字、字符串、Email.
查看>>
[leetcode]4Sum
查看>>
POJ1062 昂贵的聘礼
查看>>
【零基础学习iOS开发】【02-C语言】08-基本运算
查看>>
Java 将指定字符串连接到此字符串的结尾 concat()
查看>>
Hibernate Criterion
查看>>
Python知识
查看>>
我们为什么要搞长沙.NET技术社区(三)
查看>>
杭电acm Cake
查看>>
js函数中this的指向
查看>>
c++ 引用方式传递数组
查看>>
HBase学习之路 (九)HBase phoenix的使用
查看>>
LeetCode() Remove Duplicates from Sorted Array II
查看>>
【svn】idea svn 文件上会出现一个破书
查看>>
cocos2d-x 3.0 场景切换特效汇总(转)
查看>>
The SortedMap Interface
查看>>
SniperOJ-leak-x86-64
查看>>
bzoj 4260: Codechef REBXOR (01 Trie)
查看>>
学好python
查看>>