Leetcode---72. Edit Distance

Source: Internet
Author: User

Title Link: Edit Distance

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:

The requirement for this problem is to calculate the distance of two words, which is the minimum number of steps required to convert from Word1 to Word2. There are three things you can do: Insert characters, delete characters, and replace characters.

Calculates the distance between words, so that D[i, j] represents the distance between the first I characters of Word1 and the first J characters of Word2. The value of D[i, J] has the following conditions:

    1. If word1[i-1] equals word2[j-1], it indicates that the position does not require an operation, i.e. D[i, j] = D[i-1, j-1];
    2. If WORD1[I-1] is not equal to word2[j-1], you may need to delete or add or replace characters. If you need to delete or add characters (delete and add are actually the opposite, just consider deleting them here). You can choose to delete word1[i-1], so D[i, J] is word1 the distance of the former I-1-bit and Word2 's former J-Bits plus 1-step delete operations, namely D[i, j] = D[i-1, j] + 1. If delete word1[j-1], then D[i, j] = D[i, j-1] + 1. Of course, if replaced, then D[i, j] = D[i-1, j-1] + 1. Therefore, d[i, j] = min (D[i-1, j], D[i, j-1], d[i-1, j-1]) + 1.

Time complexity: O (MN)

Space complexity: O (MN)

1 class Solution2 {3  Public:4     int mindistance(string word1, string Word2)5     {6         int L1 = word1.size(), L2 = Word2.size();7         8         if(L1 == 0 || L2 == 0)9             return Max(L1, L2);Ten          One         Vector<Vector<int> > Vvi(L1 + 1, Vector<int>(L2 + 1, 0)); A          -          for(int I = 0; I <= L1; ++ I) -             Vvi[I][0] = I; the          for(int J = 0; J <= L2; ++ J) -             Vvi[0][J] = J; -          -          for(int I = 1; I <= L1; ++ I) +              for(int J = 1; J <= L2; ++ J) -                 if(word1[I - 1] == Word2[J - 1]) +                     Vvi[I][J] = Vvi[I - 1][J - 1]; A                 Else at                     Vvi[I][J] = min(min(Vvi[I - 1][J], Vvi[I][J - 1]), Vvi[I - 1][J - 1]) + 1; -          -         return Vvi[L1][L2]; -     } - };

This is just a simple way to realize the dynamic planning of the idea, if you want to study in detail, see this PDF.

Reprint please indicate source: Leetcode---72. Edit Distance

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.