zoukankan      html  css  js  c++  java
  • [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];
        }
    }
  • 相关阅读:
    插入排序-Java
    选择排序-java
    逻辑回归----梯度上升
    logistic回归----- 随机梯度下降法
    JAVA实现聚类指标的计算Purity、NMI、RI、Precision、Recall、F值。
    Python 条形图绘制
    java中接口的注意事项
    算法-双向队列
    算法-manacher-最长回文子串-1
    算法-kmp-1
  • 原文地址:https://www.cnblogs.com/airwindow/p/4762951.html
Copyright © 2011-2022 走看看