[Leetcode click to take notes] edit distance

Source: Internet
Author: User

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

 

Problem: dynamic planning.

Use the array DP [I] [J] to represent the minimum transformation from word1 [0, I] to word2 [0, J.

So DP [I] [J] = word1 (I) = word2 (j )? DP [I-1] [J-1]: min (DP [I-1, J], DP [I, J-1], DP [I-1, J-1]) + 1;

Special attention should be paid to the handling problems of the first row and the first column. One way is to set the DP size to DP [word1.length + 1] [word2.length + 1], then DP [0] [I] = DP [I] [0] = I, which indicates that the conversion from an empty string to a string with the length of I requires at least I steps. I personally think this is a better method, but I didn't think of it at the time.

I think of another method, and it has been stuck here for a long time. Behavior example: DP [0] [I] = math. Max (DP [0] [I-1] + (wordchars1 [0] = wordchars2 [I]? ), I); indicates that if the 0th characters of word1 are equal to the I character of word2, the DP [0] [I-1] Step transformation is required. But there is a problem here, that is, when word1 = "ppneumonia", word2 = "up", it takes four steps to transform from string "U" to "pneum, when calculating the transformation from "U" to "ppneumonia", if you use DP [0] [I-1] To get 4 steps, but this is impossible, because the length of the two strings is 5 different, it takes at least 5 steps, so we can use the max function to determine that the minimum number of steps for mutual conversion between the two strings is no less than the length difference between the two strings.

The final code is as follows:

 1 public class Solution { 2     public int minDistance(String word1, String word2) { 3         int m = word1.length(); 4         int n = word2.length(); 5         if(m == 0) 6             return n; 7         if(n == 0) 8             return m; 9         int[][] dp = new int[m][n];10         11         char[] wordchars1 = word1.toCharArray();12         char[] wordchars2 = word2.toCharArray(); 13         14         dp[0][0] = wordchars1[0] == wordchars2[0]?0:1;15         for(int i = 1;i < n;i++)16             dp[0][i] = Math.max(dp[0][i-1] + (wordchars1[0]== wordchars2[i]?0:1),i);17         for(int i = 1;i < m;i++)18                dp[i][0] = Math.max(dp[i-1][0] + (wordchars1[i]== wordchars2[0]?0:1),i);19         20         for(int i = 1;i < m;i++){21             for(int j = 1;j < n;j++){22                 if(wordchars1[i] == wordchars2[j] )23                     dp[i][j] = dp[i-1][j-1];24                 else{25                     dp[i][j] = 1 + Math.min(dp[i-1][j-1],Math.min(dp[i-1][j],dp[i][j-1]));26                 }27             }28         }29         30         return dp[m-1][n-1];31     }32 }

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.