(Daily algorithm) Leetcode -- Edit Distance (editing Distance)

Source: Internet
Author: User

(Daily algorithm) Leetcode -- Edit Distance (editing Distance)

Simply put, it is the minimum number of steps for converting a string s1 to another string s2 only through insert, delete, and substitute operations. It is easy for people familiar with algorithms to know that this is a dynamic planning problem.

In fact, a replacement operation can be equivalent to a delete + insert operation, so we define the weight 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 sum cost = 5


Edit DistanceUsed to measure the similarity between two strings. Between two strings Minimum edit distanceIt refers to the minimum operand for converting one string to another by editing (including insert, delete, and replace operations. As shown in, d (deletion) indicates the delete operation, s (substitution) indicates the replace operation, and I (insertion) indicates the insert operation. (For the sake of simplicity, Edit Distance is abbreviated as ED.) If the cost (cost) of each operation is 1, then ED = 5.

We define D (I, j) as the first I character of X [1... i] and Y's first j characters Y [1... j], where 0

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + yOe5 + 8Tcz + u1vcrHtq/MrLnmu67Oyszivs2yu8TRveK + 9sHLoaM8L3A + large "brush: java;"> 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 planning, we do not want to use the space complexity of O (m * n.

The space complexity can be reduced to O (min (m, n) By scrolling an array ))




Reference URL: editing distance-Natural Language Processing editing distance-zhangshen



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.