Delete Operation for Two Strings

Question

Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in each step you can delete one character in either string.

Example 1:

Input: "sea", "eat"
Output: 2
Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea".

Note:

  1. The length of given words won't exceed 500.
  2. Characters in given words can only be lower-case letters.

Tags

  • String
  • Dynamic Programming

Thought

Utilize LCS to calculate the distance.

Code

"""
Naive solution:
1. Run LCS for the strings
2. compare the LCS result with the length of the strings
"""

class Solution(object):
    def LCS(self, string1, string2):
        DP = [[0 for i in xrange(len(string2) + 1)] for j in xrange(len(string1) + 1)]
        for i in xrange(1, len(string1) + 1):
            for j in xrange(1, len(string2) + 1):
                if string1[i - 1] == string2[j - 1]:
                    DP[i][j] = DP[i - 1][j - 1] + 1
                else:
                    DP[i][j] = max(DP[i - 1][j], DP[i][j - 1])
        return DP[-1][-1]




    def minDistance(self, word1, word2):
        """
        :type word1: str
        :type word2: str
        :rtype: int
        """
        length_LCS = self.LCS(word1, word2)
        result = len(word1) - length_LCS
        result += len(word2) - length_LCS
        return result

results matching ""

    No results matching ""