[Leetcode] edit distance

Source: Internet
Author: User
Edit distance

Given two wordsWord1AndWord2, Find the minimum number of steps required to convertWord1ToWord2. (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

The original question in the beauty of programming, but the answer in the book is Recursive Implementation, but I don't know whether the original author gives the answer is not tested?

Very small cases will time out, for example: "trinitrophenylmethylnitramine", "dinitrophenylhydrazine"

Recursive version:

 1 public class Solution { 2     public int minDistance(String word1, String word2) { 3         if(word1 == null || word1.length() == 0) return word2.length(); 4         if(word2 == null || word2.length() == 0) return word1.length(); 5         if(word1.length() > word2.length()) return minDistance(word2,word1);//assume word1 is shorter than 2 6         if(word1.charAt(0) == word2.charAt(0)){ 7           return minDistance(word1.substring(1),word2.substring(1));   8         }else { 9             int delete = minDistance(word1,word2.substring(1)) + 1;10             int change = minDistance(word1.substring(1),word2.substring(1)) + 1;11             return Math.min(delete,change);12         }13     }14 }   
View code

 

This question is explained in detail in Wikipedia. The specific implementation uses DP:

DP algorithm:

Maintain a two-dimensional matrix to record the distance status:
Dinstance [I] [J] represents the string word1 [0 ~ I] and word2 [0 ~ J] distance
Here we need to open distance to [word1.length () + 1] [word2.length () + 1]

[0] [0] indicates that when both are empty strings, distance is obviously 0.
When I = 0, distance [0] [J] = J (where 1 <= j <= word2.length (), similarly
When J = 0, distance [I] [0] = I (1 <= I <= word1.length () and distance [I] [J]
When word1.charat (I) = word2.charat (J,
Apparently distance [I] [J] = distance [I-1] [J-1];
When word1.charat (I )! = Word2.charat (J,
We need to check that distance [I-1] [J-1], distance [I] [J-1], and distance [I-1] [J] correspond to three situations: change word1 [I] To word2 [J], delete word2 [J], delete word1 [I], and find the smallest number of the three, then + 1 (indicates the delete operation or modify Operation)

The Code is as follows:

 1 public class Solution { 2     public int minDistance(String word1, String word2) { 3         if(word1 == null || word1.length() == 0) return word2.length(); 4         if(word2 == null || word2.length() == 0) return word1.length(); 5         if(word1.length() > word2.length()) return minDistance(word2,word1);//assume word1 is shorter than 2 6         int height = word1.length() + 1,width = word2.length() + 1; 7         int[][] dp = new int[height][width]; 8         for(int i = 0; i < width;i++){ 9             if(i < height){10                 dp[i][0] = i;11             }12             dp[0][i] = i;13         }14         for(int i = 1; i < height ; i++){15             for(int j = 1; j < width ; j++){16                 if(word1.charAt(i - 1) == word2.charAt(j - 1)){17                     dp[i][j] = dp[i - 1][j - 1];18                 }else{19                     dp[i][j] = Math.min(dp[i - 1][j - 1], Math.min(dp[i][j - 1], dp[i - 1][j])) + 1;20                 }21             }22         }23         return dp[word1.length()][word2.length()];24     }25 }   

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.