[Leetcode Series] [algorithm] [difficult] edit distance (DP)

topic:

Topic links:  https://leetcode-cn.com/problems/edit-distance/

 

Problem-solving ideas:

Dynamic programming, in accordance with the meaning of the questions, the operation can be carried out as follows:

Operation can be performed:

  1. Inserting a character in A
  2. To delete a character in A
  3. Modify a character in A
  4. Inserting a character in B
  5. To delete a character in B
  6. Modify a character in B

However, some of which are equivalent operation effects:

  1. Inserting a character in A = B to delete a character, such as A = '12', B = '123', is inserted in A '3', B is equivalent to delete the '3'
  2. Inserting a character in the B = A delete a character, such as A = '123', B = '12', insert '3' in B, the same as deleting in A '3'
  3. A modification of one character in a character B = modified, such as A = '123', B = '124',

So in the end, we only need to consider the following three operations on it:

  1. Inserting a character in A
  2. Inserting a character in B
  3. Modify a character in A

Why did you choose these three, rather than the other three, it goes on to say

Thinking this way, we put a big question, converted into a small problem, but does not meet the state transition equation of dynamic programming, the need to continue dismantling, dismantling as follows:

In each of the above operations, the impact on the total number of operations is as follows:

  1. Inserting a character in A: assumed from '123' to a '456' step requires an X, then ask from '123' converter '4567' number of steps required, no more than X + 1 steps, because the '123' convert to '456', the only need more behind a '7', the conversion to complete
  2. Inserting a character in B: Suppose from '123' to a '456' need Y steps, then from '1234' to a '456' number of steps required, no more than Y + 1 steps, because the '123' convert to '456', the back plus a '4', to complete the conversion
  3. Modify a character in A: assumed from '123' to a '456' Z steps required, then from '1234' to a '4567' number of steps required, no more than Z + 1 steps, because the '123' convert to '456', the then only '4' modify '7', the conversion to complete

A character into it from the number of steps desired character B, is transformed into min (X + 1, Y + 1, Z + 1)

According to the above operation, just know that an empty string into a much needed step B, the delivery can be launched from A B string into a number of steps required; this is why only choose to add character and reason to modify the operation of the above character, because in dynamic programming process, only needs to be considered from less to more recursive process can be, and need not be considered from more to less

Process illustrated as follows:

If the 0th row represents an empty string, into corresponding positions 'ros' strings you need to carry out step operands; a 0-th column, represents an empty string, into a corresponding step position 'horse' string number. Because the empty string into a corresponding character string, the character can only be inserted into the corresponding operation, the number of operations is the number of characters, the initialization dp [i] [0] = i, dp [0] [j] = j,

Next, the traversing position [1,1], then [1, 1] lattice near the correspondence and the operation is as follows:

  1. Left lattice [0,1] = B, insert a character by the = 'character into an' r 'character, changed from' h 'is converted into character' r 'character
  2. Lower lattice [1,0] = inserting a character in A = a 'h' is converted to the character '' character changed from 'h' is converted into character 'r' character
  3. The lower left corner of the grid [0,0] = modify a character in A = a 'character into' character, changed from 'h' character, is converted to 'r' character

Therefore, update logic [1,1] position dp [1] [1] = min (dp [0] [1] + 1, dp [1] [0] + 1, dp [0] [0] + 1) = 1 + min (dp [0] [1], d [1] [0], dp [1] [1]) = 1

According to this logic continues to traverse:

At this time, when traversal of the next character, two characters of the same letter are found, are 'O', so for 'modify a character in A' operation, i.e. the lower left corner [1,1] processing is not additional operations, this time is updated as follows:

According to this logic continues the cycle is derived:

After the completion of traversal illustrated as follows:

As can be seen from the above illustration, our dynamic programming state transition equation is:

dp[i][j] = \left\{\begin{matrix} 1 + min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) \quad if A[i]\neq B[j]\\ 1 + min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1] - 1) \quad if A[i] = B[j] \end{matrix}\right.

Where A [i] denotes the i A character, B [j] is B j-th character in

 

Code:

class Solution:
    def minDistance(self, word1: str, word2: str) -> int:
        m = len(word1)
        n = len(word2)
        if m * n == 0:
            return m + n
        
        dp = [[0] * (m + 1) for _ in range(n + 1)]
        
        for i in range(n + 1):
            dp[i][0] = i
            
        for j in range(m + 1):
            dp[0][j] = j
            
        for i in range(1, n + 1):
            for j in range(1, m + 1):
                left = dp[i][j - 1] + 1
                down = dp[i - 1][j] + 1
                left_down = dp[i - 1][j - 1] + 1
                if word2[i - 1] == word1[j - 1]:
                    left_down -= 1
                    
                dp[i][j] = min(left, down, left_down)
                
        return dp[n][m]
            

 

 

 

 

 

Published 100 original articles · won praise 4 · Views 1456

Guess you like

Origin blog.csdn.net/songyuwen0808/article/details/105408237