Finding minimum moves required for making 2 strings equal
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Problem Overview
In computational string theory, one common problem is determining the minimum number of moves required to make two strings identical. A "move" can traditionally be defined as an insertion, deletion, or substitution of a single character. The goal is to transform one string into another with the fewest moves possible.
Key Concept: Edit Distance
The problem of finding the minimum number of moves is known in computer science as calculating the "Edit Distance." The most widely used algorithm to solve this is the Levenshtein Distance algorithm.
The Levenshtein Distance between two strings is defined as the minimum number of single-character edits (insertions, deletions, or substitutions) required to transform one string into the other.
Dynamic Programming Approach
The dynamic programming approach to finding the Edit Distance involves creating a two-dimensional table to store the minimum edit distances between all prefixes of the first string and all prefixes of the second string.
Algorithm
Given two strings, `str1` and `str2`, of lengths `m` and `n` respectively, create a table `dp` of size `(m+1) x (n+1)`, where `dp[i][j]` represents the edit distance between `str1[0...i-1]` and `str2[0...j-1]`. Here's how to fill the table:
- Initialization:
• `dp[0][0]` = 0 • `dp[i][0]` = i (since transforming any non-empty string to an empty string requires i deletions) • `dp[0][j]` = j (since transforming an empty string to any non-empty string requires j insertions) - Filling the Table:
For every character in `str1` and `str2` do:
If `str1[i-1] == str2[j-1]`:
`dp[i][j] = dp[i-1][j-1]`
Else:
`dp[i][j] = 1 + \min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1])` - Final Answer:
• The value in `dp[m][n]` will be the minimum edit distance for `str1` and `str2`.
Time and Space Complexity
• Time Complexity: • Space Complexity: , though it can be optimized to using only two arrays to store the current and previous rows.
Example
Consider two strings:
• `str1 = "sunday"` • `str2 = "saturday"`
To transform `str1` into `str2`, the dynamic programming table is filled as follows:
| s | a | t | u | r | d | a | y | ||
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
| s | 1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| u | 2 | 1 | 1 | 2 | 2 | 3 | 4 | 5 | 6 |
| n | 3 | 2 | 2 | 2 | 3 | 3 | 4 | 5 | 6 |
| d | 4 | 3 | 3 | 3 | 3 | 4 | 3 | 4 | 5 |
| a | 5 | 4 | 3 | 4 | 4 | 4 | 4 | 3 | 4 |
| y | 6 | 5 | 4 | 4 | 5 | 5 | 4 | 4 | 3 |
The result gives us an edit distance of 3, indicating three operations (insertions or deletions) are needed.
Additional Considerations
• Handling Case Sensitivity: Ensure strings are treated with the intended sensitivity (case-sensitive vs. case-insensitive). • Unicode Characters: Ensure your algorithm supports the Unicode standard if necessary. • Approximate Matching: Sometimes, it's useful to allow for near matches, using a predefined threshold on the edit distance.
Summary Table
| Key Points | Details |
| Problem Objective | Minimize moves to make two strings equal |
| Key Algorithm | Levenshtein Distance (Edit Distance) |
| Approach | Dynamic Programming |
| Time Complexity | |
| Space Complexity | (optimizable to ) |
| Example Used | "sunday" to "saturday": 3 moves |
By understanding and applying the edit distance algorithm, we can efficiently and effectively solve the problem of transforming two strings to be identical with minimal operations.

