Edit Distance (or Levenshtein Distance) Python solution for Leetcode EPI 17.2

Source: Internet
Author: User

https://oj.leetcode.com/problems/edit-distance/

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:

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

1 classSolution:2     #@return An integer3     defmindistance (self, Word1, Word2):4         #http://www.cnblogs.com/zuoyuan/p/3773134.html5M=len (word1) +1; N=len (WORD2) +16DP = [[0 forIinchRange (n)] forJinchrange (m)]7Delcost = Inscost = Subcost = 1#The cost of each operation8         9          forIinchRange (m):Tendp[i][0]=I One          forJinchrange (n): Adp[0][j]=J -          -          forIinchRange (1, m): the              forJinchRange (1, N): -                 #del Insert same sub -                 #dp[i][j]=min (dp[i-1][j]+1, dp[i][j-1]+1, dp[i-1][j-1]+ (0 if word1[i-1]==word2[j-1] else 1)) -Dp[i][j]=min (Dp[i-1][j] + inscost, dp[i][j-1] + delcost, dp[i-1][j-1]+ (0ifWORD1[I-1]==WORD2[J-1]Elsesubcost)) +         returnDp[m-1][n-1] -          +         """ A Problem Solving idea: This question is a very famous editing distance question. Use dynamic programming to solve.  at The state transition equation is this: Dp[i][j] represents the editing distance of word1[0...i-1] to word2[0...j-1].  - and dp[i][0] is obviously equal to I, because only need to do I delete operation is OK.  - The same is true for dp[0][i], equal to I, because I can only do the insertion operation.  - Dp[i-1][j] to dp[i][j] need to add 1, because word1[0...i-2] to word2[0...j-1] The distance is dp[i-1][j], - and Word1[0...i-1] to word1[0...i-2] need to perform a delete, so dp[i][j]=dp[i-1][j]+1; - The same dp[i][j]=dp[i][j-1]+1, because you also need to add a word2 insert operation.  in if word[i-1]==word[j-1], then dp[i][j]=dp[i-1][j-1], - if word[i-1]!=word[j-1], then you need to perform a replacement replace operation, so dp[i][j]=dp[i-1][j-1]+1, the above is the derivation of the state transition equation.  to         """ + " "Implement The minimum edit distance according to - The description of WikiPedia: the http://en.wikipedia.org/wiki/Edit_distance * Some Other implements is available here: $ http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distancePanax Notoginseng         " " -         #If Len (word1) < Len (word2): the         #word1, Word2 =word2, Word1 +         """ A delcost = Inscost = Subcost = 1 # The cost of each operation the steps = Range (len (word1) +1) +          - For Word2index in Xrange (Len (word2)): $ diag = steps[0] # d (i-1, j-1) $ Steps[0] + = 1 -              - For Word1index in Xrange (Len (word1)): the temp = steps[word1index+1] # d (I-1, J) -                 Wuyi if word1[word1index] = = Word2[word2index]: the steps[word1index+1] = diag - Else: Wu steps[word1index+1] = min (steps[word1index+1] + inscost, - Steps[word1index] + delcost, About diag + subcost) $                  - diag = temp # Store D (i-1, j-1) for next cell -                      - return steps[-1] A         """

Edit Distance (or Levenshtein Distance) Python solution for Leetcode EPI 17.2

Related Article

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.