Advertisement

Edit Distance - LeetCode 72 Solution

Edit Distance - Complete Solution Guide

Edit Distance is LeetCode problem 72, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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: horse -> rorse (replace 'h' with 'r') rorse -> rose (remove 'r') rose -> ros (remove 'e') Example 2: Input: word1 = "intention", word2 = "execution" Output: 5 Explanation: intention -> inention (r

Detailed Explanation

The Edit Distance problem asks us to find the minimum number of operations (insert, delete, or replace) needed to transform one string (word1) into another string (word2). We are given two strings as input, and we need to return the smallest number of operations to make them identical. The constraints limit string lengths to a maximum of 500 characters consisting of lowercase English letters.

Solution Approach

The provided solution uses dynamic programming to solve the Edit Distance problem. It builds a 2D table `dp` where `dp[i][j]` represents the minimum number of operations to transform the first `i` characters of `word1` into the first `j` characters of `word2`. The algorithm iterates through the table, filling it based on the following logic: 1. **Base Cases:** The first row and column of the table are initialized. `dp[i][0] = i` because to convert the first `i` characters of `word1` to an empty string, we need `i` deletions. Similarly, `dp[0][j] = j` because to convert an empty string to the first `j` characters of `word2`, we need `j` insertions. 2. **Recursive Relation:** For each cell `dp[i][j]`, the algorithm checks if `word1[i-1]` is equal to `word2[j-1]`. If they are equal, it means no operation is needed, so `dp[i][j] = dp[i-1][j-1]`. If they are not equal, it considers three operations: insert, delete, and replace. The algorithm then takes the minimum of these three operations and adds 1 (for the operation itself) to get the value of `dp[i][j]`. 3. **Final Result:** The final answer, the minimum edit distance between `word1` and `word2`, is stored in `dp[m][n]`, where `m` is the length of `word1` and `n` is the length of `word2`.

Step-by-Step Algorithm

  1. Step 1: Create a 2D DP table `dp` of size (m+1) x (n+1), where m and n are the lengths of word1 and word2 respectively.
  2. Step 2: Initialize the first row and first column of `dp`. dp[i][0] = i for all i from 0 to m, and dp[0][j] = j for all j from 0 to n.
  3. Step 3: Iterate through the remaining cells of `dp` (from i=1 to m and j=1 to n).
  4. Step 4: For each cell dp[i][j], compare word1[i-1] and word2[j-1].
  5. Step 5: If word1[i-1] == word2[j-1], then dp[i][j] = dp[i-1][j-1].
  6. Step 6: If word1[i-1] != word2[j-1], then dp[i][j] = 1 + min(dp[i][j-1], dp[i-1][j], dp[i-1][j-1]). This considers insert, delete and replace operations, respectively.
  7. Step 7: After filling the entire table, return dp[m][n].

Key Insights

  • Insight 1: Dynamic Programming is essential because we can break down the problem into smaller overlapping subproblems. The optimal solution for transforming a prefix of word1 to a prefix of word2 can be built from the optimal solutions of smaller prefixes.
  • Insight 2: The core idea is to consider the last characters of both strings. If they are the same, no operation is needed. If they are different, we consider insert, delete, and replace, and pick the operation that results in the minimum total operations.
  • Insight 3: We can use a 2D DP table `dp[i][j]` to store the minimum edit distance between the first `i` characters of `word1` and the first `j` characters of `word2`.

Complexity Analysis

Time Complexity: O(mn)

Space Complexity: O(mn)

Topics

This problem involves: String, Dynamic Programming.

Companies

Asked at: Accenture, Adobe, Amazon, Apple, Arcesium, Axon, Bloomberg, Cisco, DE Shaw, Deloitte, Flipkart, HashedIn, Infosys, LinkedIn, MakeMyTrip, Meta, Microsoft, Nielsen, Rubrik, Snap, Swiggy, TikTok, Visa, Yahoo, Zoho, tcs.