(Daily algorithm) Leetcode--edit Distance (Edit distance)

Source: Internet
Author: User

Simply put, it is the minimum number of steps to transform one string S1 to another s2 by inserting (insert), deleting (delete), and replacing (substitute) operations. The students who are familiar with the algorithm can easily know that this is a dynamic planning problem.

In fact, a substitution operation can be equivalent to a delete+ an insert, so we define the weights as follows:

I (insert): 1

D (delete): 1

S (substitute): 1


Example:

Intention->execution

Minimal Edit Distance:

Delete I; N->e; T->x; Insert C; N->u summation cost=5


Edit DistanceUsed to measure the similarity between the two strings. Two strings between the two Minimum Edit Distanceis the smallest operand that converts one of the strings to another string by editing (including insert, delete, and replace operations). As shown, d (deletion) represents the delete operation, S (substitution) represents the replace operation, and I (insertion) represents the insert operation. (For simplicity, the later edit Distance is ed) if the cost of each operation is 1, then ed = 5.

We define the distance between D (I,J) for the first I character of X X[1...i] and the first J character Y[1...j] of y, where 0<i<n, 0<j<m, so the distance between x and Y can be represented by D (n,m). If we want to calculate the final D (n,m), then we can start from scratch, calculate the values of D (i, j) (I and J starting from 1), and then calculate the larger D (I, j) based on the previous results until D (N,M) is finally evaluated.

If you can think of a dynamic planning problem is not difficult to solve.

Class Solution {public:    int mindistance (string word1, String word2) {        const size_t n = word1.size ();        Const size_t m = word2.size ();        int F[n + 1][m + 1];        for (size_t i = 0; I <= N; i++)        f[i][0] = i;        for (size_t j = 0; J <= M; j + +)        f[0][j] = j;        for (size_t i = 1; I <= N, i++)        {for        (size_t j = 1; j <= M; j + +)        {        if (word1[i-1] = = Word2[j-1] )        f[i][j] = f[i-1][j-1];        else        {        int mn = min (f[i-1][j], f[i][j-1]);        F[i][j] = min (f[i-1][j-1], MN) + 1;}}        }        return f[n][m];}    ;

Similarly, for dynamic programming problems, we do not want to use the spatial complexity of O (m*n).

The spatial complexity can be reduced to O (min (m, n)) by scrolling the array.




Reference URL: Editing distance-natural language processing editing distance-Zhang



(Daily algorithm) Leetcode--edit Distance (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.