Leetcode 72. Edit Distance

Source: Internet
Author: User

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

[Solution]

Use Table[i][j] to represent the minimum steps required to change from word1[i] to word2[j].

1) Match: If WORD1[I]=WORD2[J], then simply turn word1[0...i-1] into word2[0...j-1]. ==> Table[i][j] = table[i-1][j-1].

2) Replace: Replace the word1[i] with Word2[j] is the smallest change from word1 to Word2, then the word1[0...i-1] is changed to the minimum cost word2[0...j-1], and then replaced. ==> Table[i][j] = table[i-1][j-1] + 1.

3) Insert: If in word1[i] insert a character is word1 to word2[minimum change, then word1[0...i] to the minimum cost to word2[0...j-1], on this basis add word2[j], ==> table[i][j] = TABLE[I][J-1] + 1.

4) Delete: If delete word1[i] is word1 to word2 minimum change, then word1[0...i-1] to the minimum cost to WORD2[0...J], and then delete word1[j], table[i][j] = Table[i-1][j] + 1.

1 intMindistance (stringWord1,stringWord2)2     {3         intI, J, Ndelete, Ninsert, Nreplace, Min_step;4         intm = Word1.size (), n =word2.size ();5         if(M = =0|| n = =0) 6             return(M = =0) ?n:m;7             8         int**table =New int* [M +1];//table[i][j] means change word1[i] to word2[j];9          for(i =0; I <= m; i++)TenTable[i] =New int[n +1]; One          A          for(i =0; I <= m; i++) -table[i][0] =i; -          for(j =0; J <= N; J + +) thetable[0][J] =J; -                  -          for(i =1; I <= m; i++) -         {     +              for(j =1; J <= N; J + +) -             { +Ndelete = Table[i-1][J] +1; ANinsert = Table[i][j-1] +1; at                 if(Word1[i-1] = = Word2[j-1]) -Nreplace = Table[i-1][j-1]; -                 Else -Nreplace = Table[i-1][j-1] +1; -TABLE[I][J] =min (nreplace, min (ndelete, Ninsert)); -             } in         } -Min_step =Table[m][n]; to          +          for(i =0; I <= m; i++) - delete[] table[i]; the delete[] table; *          $         returnMin_step;Panax Notoginseng}

Leetcode 72. 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.