天天看點

Leetcode:Edit Distance

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

    計算兩個字元串之間的距離。第一眼看到這個題的時候,感覺很熟悉,應該在很多面試書中看到過。個人覺得這個算法應該是面試中常考的,是以應該熟悉掌握。

運用遞歸,主要算法思想為:

Algorithm:

minDistance(i,j)=1+min(minDistance(i-1,j),minDistance(i,j-1),minDistance(i-1,j-1));

用一個二維數組表示兩個字元串前n個字元之間的距離,然後使用以上算法,即可實作。實作代碼如下:

public class Solution {
    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] = 1 + Math.min(dp[i-1][j-1],Math.min(dp[i-1][j],dp[i][j-1]));
                }
            }
        }
        return dp[n][m];
    }
}