• [LeetCode#72]Edit Distance


    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_distance
    
    For 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];
        }
    }
  • 相关阅读:
    求助 大家帮忙激励下我吧
    实体培训,特别是对于学历教育中教学理论的一些总结
    天轰穿c#趣味编程系列视频 vs2005/2008 winform实例入门 第二集 学习技巧风暴
    感谢老婆的支持我永远爱你,亲爱的梅
    早上起床晚了,差点迟到,两集视频已经做好
    我带这个班最近两次的作业
    辛辛苦苦几十年,一朝回到解放前我以及我的部分亲人都平安
    天轰穿c#趣味编程系列视频 vs2005/2008 winform实例入门 第一集
    如果有媒体的朋友建议看下这个帖子
    无聊的盗版问题
  • 原文地址:https://www.cnblogs.com/airwindow/p/4762951.html
Copyright © 2020-2023  润新知