Finding minimum moves required for making 2 strings equal
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
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.
Related reading
- Finding minimum number of points which covers entire set of intervals?
- Finding minimum number of presses to shut down lamps
- Finding multiple entries with binary search
- Finding n-th permutation without computing others
- Finding nearest free position for a circle for any point x,y in a 2D space with circles
- Finding nearest point in an efficient way
- Finding neighbourhoods cliques in street data a graph
- Finding number of concurrent events given start and end times

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.