20. Make Strings Equal
β GFG solution to Make Strings Equal problem: find minimum cost to transform two strings to be identical using Floyd-Warshall algorithm for shortest path computation. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given two strings s and t consisting of lowercase English letters. You are also given a 2D array transform[][] of size n*2, where each entry [x, y] means you can transform character x into character y, and an array cost[], where cost[i] is the cost of transforming transform[i][0] into transform[i][1].
Your task is to find the minimum total cost required to make the strings identical by transforming characters. You can apply any transformation any number of times on either string. If it is impossible to make the two strings identical, return -1.
π Examples
Example 1
Input: s = "abcc", t = "bccc", transform[][] = [['a', 'b'], ['b', 'c'], ['c', 'a']], cost[] = [2, 1, 4]
Output: 3
Explanation: We can convert both strings into "bccc" with a cost of 3:
- Position 0 in s: a -> b (cost 2)
- Position 1 in s: b -> c (cost 1)
Other characters already match.Example 2
Input: s = "az", t = "dc", transform[][] = [['a', 'b'], ['b', 'c'], ['c', 'd'], ['a', 'd'], ['z', 'c']], cost[] = [5, 3, 2, 50, 10]
Output: 20
Explanation: We can convert both strings into "dc" with a cost of 20:
- Position 0 in s: a -> d by path a->b->c->d (cost 5+3+2=10)
- Position 1 in s: z -> c (cost 10)Example 3
π Constraints
$1 \le s.size() = t.size() \le 10^5$
$1 \le transform.size() = cost.size() \le 500$
$'a' \le transform[i][0], transform[i][1] \le 'z'$
$1 \le cost[i] \le 500$
β
My Approach
The optimal approach uses Floyd-Warshall Algorithm to precompute shortest transformation paths between all pairs of characters:
Floyd-Warshall with Graph Transformation
Build Distance Matrix:
Create a 26x26 matrix representing all lowercase English letters.
Initialize diagonal elements to 0 (no cost to transform character to itself).
Initialize all other distances to infinity.
Fill Direct Transformation Costs:
For each transformation rule, update the distance matrix with minimum cost.
Handle multiple transformations between same character pairs by taking minimum.
Apply Floyd-Warshall:
For each intermediate character k, update all pairs (i, j).
If path i->k->j is cheaper than direct i->j, update the distance.
This computes shortest transformation paths between all character pairs.
Find Common Target Characters:
For each mismatched position in strings, try all 26 possible common characters.
Calculate cost to transform both s[i] and t[i] to each common character.
Choose the minimum cost transformation.
Return Result:
Sum all transformation costs for mismatched positions.
Return -1 if any position cannot be resolved.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(26Β³ + n), where 26Β³ accounts for Floyd-Warshall preprocessing on the fixed 26-character alphabet, and n is the length of strings for comparing and computing transformations at each position. The preprocessing is a constant operation, making the overall complexity effectively O(n) for the string comparison phase.
Expected Auxiliary Space Complexity: O(26Β²) = O(1), as we use a fixed 26x26 distance matrix regardless of input size. This is constant space since the alphabet size is fixed.
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated