[Leetcode] Edit Distance

Source: Internet
Author: User

Topic:

Given 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

To find the string edit distance (edit Distance), the editing distance refers to the minimum number of edit operations required to turn from one two string to another. Permission edits include a) insert a character B) delete one character C) replace one character with another character such as: Tea->eat the minimum two steps.  Analysis process: Assuming that two strings are S and T, enumeration string s and T last character S[i], T[j] corresponds to four cases: (character-character) (character-blank) (Blank-character) (blank-blank); Obviously, (blank-blank) is necessarily an extra edit operation. (1)    s + blank    t + character x     Then, S becomes T, finally, insert "character x" dp[i,j] = dp[i,j-1] + 1  (2) s + character X T + character y at the end of S  s becomes T, finally, in X modified into y         DP[I,J] = Dp[i-1,j-1] + (x==y? 0:1)       (3) S + Character x    t + blank    s to t,x removed    dp[i,j] = Dp[i-1,j] + 1 with Dp[i][j] for first string s[0...i] and 2nd string t[0    The shortest editing distance of ... j]. Then    if (S[i]==t[j])  dp[i][j] = min (dp[i-1][j-1],dp[i][j-1]+1,dp[i-1][j-1]+1) else                  DP[I][J] = min (dp[i-1][j],dp[i][j-1],dp[i-1][j-1]) +1 The final code is as follows:
Class Solution {public:    int mindistance (string word1, String word2) {int len1 = word1.length (); int len2 = Word2.length () if (len1 = = 0) return len2;if (Len2 = = 0) return Len1;int i,j;vector<vector<int> > dp (len1 + 1, vector<int > (len2 + 1)); for (i = 0; I <= len1; i++) {dp[i][0] = i;} for (j = 0; J <= Len2; j + +) {Dp[0][j] = j;} for (i = 1, i <= len1; i++) {for (j = 1; J <= Len2; j + +) {/*int cost = (word1[i-1] = = Word2[j-1])? 0:1;dp[i][j] = m In (Dp[i-1][j-1]+cost,min (dp[i-1][j]+1,dp[i][j-1]+1)); */if (word1[i-1] = word2[j-1]) {dp[i][j] = min (dp[i-1][j-1], Min (dp[i-1][j],dp[i][j-1]) + 1);} Else{dp[i][j] = min (dp[i-1][j-1],min (dp[i-1][j],dp[i][j-1])) + 1;}}} return dp[len1][len2];}    ;

  

[Leetcode] Edit Distance

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.