Algorithms

Edit Distance

10 months, 2 weeks ago ; 117 views
Share this

Edit Distance

 

Given two strings, word1, and word2, return the minimum number of operations required to convert word1 to word2.

 

You have the following three operations permitted on a word:

  • insert a character
  • delete a character
  • replace a character

 

Example 1:

Input: word1 ="horse", word2="ros"

Output: 3

Explanation:

  1. horse -> rorse (replace 'h' with 'r')
  2. rorse -> rose (remove 'r')
  3. rose -> ros (remove 'e')

 Solution

def word_convert(self, word1: str, word2: str) -> int:

        def dfs(i, j, count):
            # Base case 1: If word2 is empty, we need to delete remaining characters in word1.
            if j == len(word2):
                return count + len(word1) - i

            # Base case 2: If word1 is empty, we need to insert remaining characters from word2.
            if i == len(word1):
                return count + len(word2) - j

            # Base case 3: If the characters match, just move to the next character.
            if word1[i] == word2[j]:
                return dfs(i + 1, j + 1, count)

            # Apply the three operations: replace, delete, insert.
            replace_op = dfs(i + 1, j + 1, count + 1)  # Replace
            delete_op = dfs(i + 1, j, count + 1)        # Delete
            insert_op = dfs(i, j + 1, count + 1)        # Insert

            # Return the minimum cost among these three operations.
            return min(replace_op, delete_op, insert_op)

        return dfs(0, 0, 0)

Code Walkthrough

Here's a detailed explanation of the code:

The word_convert function takes two strings, word1, and word2, as input.

It initializes a recursive function dfs that explores the conversion from word1 to word2.

The function takes three parameters: i and j represent the current positions in word1 and word2, respectively, and count represents the current cost (the number of operations).

 

There are three base cases:

Base Case 1: If j has reached the end of word2, it means there are characters left in word1 that need to be deleted. The function returns the count plus the remaining characters in word1 (len(word1) - i).

Base Case 2: If i has reached the end of word1, it means there are characters left in word2 that need to be inserted into word1. The function returns the count plus the remaining characters in word2 (len(word2) - j).

Base Case 3: If the characters at the current positions i and j in word1 and word2 match, this character has no cost. The function recursively calls itself with i and j incremented by 1 and the same count.

If the characters at i and j do not match, the function explores three possible operations:

  1. Replace Operation: It makes a recursive call with i and j incremented by 1 and count incremented by 1.
  2. Delete Operation: It makes a recursive call with only i incremented by 1 and count incremented by 1.
  3. Insert Operation: It makes a recursive call with only j incremented by 1 and count incremented by 1.

The function returns the minimum cost among these three operations (replace, delete, insert) by using min(replace_op, delete_op, insert_op).

The main word_convert function starts the recursion by calling dfs with initial values: i=0, j=0, and count=0.

The final result of the dfs function is the minimum cost required to convert word1 to word2, considering replacement, deletion, and insertion operations.

This code uses recursion to explore all possible conversion paths, and the result is the minimum cost required to transform one word into another.

Time Complexity

The time complexity of this algorithm is exponential, specifically O(3^N), where N is the longer length of the two input words, word1 or word2.

The reason for the exponential time complexity is that the algorithm explores all possible combinations of replacements, deletions, and insertions, resulting in a branching factor of 3 at each character position.

 

Space Complexity

The space complexity is determined by the depth of the recursion, which can go as deep as the length of the longer word. Therefore, the space complexity is O(N), where N is the maximum length between word1 and word2.

This space complexity accounts for the call stack memory used during the recursion.

 

Become a member
Get the latest news right in your inbox. We never spam!

Read next

Island Perimeter

 This solution seeks to find the perimeter of an island in a grid.  The problem considers a grid where each cell represents land (1) or … Read More

Kibsoft 3 months, 3 weeks ago . 76 views

Pacific Atlantic Waterflow

3 months, 3 weeks ago . 82 views