Edit Distance (convert two one string to another in minimum opearation)

 

Intuition :

  • Here we have to find the minimum edit distance problem between two strings word1 and word2.
  • The minimum edit distance is defined as the minimum number of operations required to transform one string into another.

Approach :

  • The approach here that I am using is dynamic programming. The idea is to build a 2D matrix dp where dp[i][j] represents the minimum number of operations required to transform the substring word1[0...i-1] into the substring word2[0...j-1].

How is Matrix built :

  • The matrix is built iteratively using the following recurrence relation:
  1. If word1[i-1] == word2[j-1], then dp[i][j] = dp[i-1][j-1]. That is, no operation is required because the characters at positions i-1 and j-1 are already the same.
  2. Otherwise, dp[i][j] is the minimum of the following three values:
  • dp[i-1][j-1] + 1: replace the character at position i-1 in word1 with the character at position j-1 in word2.
  • dp[i-1][j] + 1: delete the character at position i-1 in word1.
  • dp[i][j-1] + 1: insert the character at position j-1 in word2 into word1 at position i.

The base cases are:

  • dp[i][0] = i: transforming word1[0...i-1] into an empty string requires i deletions.
  • dp[0][j] = j: transforming an empty string into word2[0...j-1] requires j insertions.

Final Step :

  • Finally, return dp[m][n], which represents the minimum number of operations required to transform word1 into word2, where m is the length of word1 and n is the length of word2.

Complexity

  • Time complexity : O(mn)
  • Space complexity : O(mn)

Comments

Popular posts from this blog

30. Substring with Concatenation of All Words

Lowest common ancestor in a binary Tree

New start day 1 : 30/09/2020 (codeforces 71A) Way to long strings