Leetcode 72. Edit Distance Dynamic Programming

Source: Internet
Author: User
ImpressionsThis is what I see on the cattle online a very classic dynamic planning topic, I was not very understanding, here I put it down, as far as possible to write detailed. problemGiven two words word1 and word2, find the minimum number of steps required to convert Word1 to Word2. (Each operation are counted as 1 step.)

You are have the following 3 operations permitted on a word:

A) Insert a character
b) Delete a character
c) Replace a character

Code
Class Solution {public
:
    int mindistance (string word1, String word2) {
        int m=word1.length ();
        int n=word2.length ();
        DP[I][J] Represents the cost
        vector<vector<int> > DP (m+1,vector<int> (n+1,0)) of the former J substring of the word1, which becomes word2;
        for (int i=1;i<=m;i++) {
            dp[i][0]=i;//Delete I chars of word1
        } for
        (int i=1;i<=n;i++) {
            dp[0][i] =i;  Insert I chars of Word2
        } for
        (int i=1;i<=m;i++) {for
            (int j=1;j<=n;j++) {
                if (word1[i-1]== Word2[j-1]) {
                    dp[i][j]=dp[i-1][j-1];
                } else{
                    dp[i][j]=min (dp[i-1][j]+1,min (dp[i][j-1]+1,dp[i-1][j-1]+1));//Delete,insert,replace
                }
            }
        }
        return dp[m][n];
    }
;

AnalysisDP[I][J] Represents the cost of the first J substring of the first I substring of the word1 to Word2, and for each modification we have a different state transition equation Replace:dp[i-1][j-1] + 1// Retain the number of times before the i-1 from the word1 to the Word2 j-1 before adding one, plus one refers to the change of the insert:dp[i][j-1] + 1//To keep the number of transitions from word1 before I to Word2, and then add a delete : Dp[i-1][j] + 1//retains the number of previous i-1 characters from Word1 to the first J of the Word2. Plus one, if it's a replacement, the first i-1 string can only be paired with the first j-1 string, and if you need to match the string I and the J string, you need +1 to replace the operation. If you are inserting, the first I string can only be paired with the first j-1 string, if you need to match the J, you need +1, insert operation if it is deleted, the i-1 characters can be paired with the first J string, the I character is superfluous, then +1, for the delete operation.
For replacement inserts or deletions, we select the smallest value BoundaryThe number of operations that are converted from an empty string to any string and from any string to a null string are changed to the length of arbitrary strings (n-time inserts or N-Deletes)
Dp[i][0] = i;
DP[0][J] = j;

Reference Documents[1]. [Programming title]edit-distance.https://www.nowcoder.com/questionterminal/81d7738f954242e5ade5e65ec40e5027 [2]. Leetcode 72. Edit Distance Shortest string editing distance dynamic planning. HTTPS://WWW.JIANSHU.COM/P/7AA3698096A1

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.