string manipulation
algorithm
minimum moves
string equality
computational problems

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.

Practice algorithms

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:

  1. 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)
  2. Filling the Table:
    For every character in `str1` and `str2` do:
    \hspace{1cm} If `str1[i-1] == str2[j-1]`:
    \hspace{2cm} `dp[i][j] = dp[i-1][j-1]`
    \hspace{1cm} Else:
    \hspace{2cm} `dp[i][j] = 1 + \min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1])`
  3. Final Answer:
    • The value in `dp[m][n]` will be the minimum edit distance for `str1` and `str2`.

Time and Space Complexity

Time Complexity: O(m×n)O(m \times n)Space Complexity: O(m×n)O(m \times n), though it can be optimized to O(min(m,n))O(\min(m, n)) 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:

saturday
012345678
s101234567
u211223456
n322233456
d433334345
a543444434
y654455443

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 PointsDetails
Problem ObjectiveMinimize moves to make two strings equal
Key AlgorithmLevenshtein Distance (Edit Distance)
ApproachDynamic Programming
Time ComplexityO(m×n)O(m \times n)
Space ComplexityO(m×n)O(m \times n) (optimizable to O(min(m,n))O(min(m, n)))
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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.