githubEdit

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 Linkarrow-up-right

🧩 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

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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++)

chevron-right⚑ View Alternative Approaches with Code and Analysishashtag

πŸ“Š 2️⃣ Dijkstra-Based Approach

πŸ’‘ Algorithm Steps:

  1. Build an adjacency list representation from transformation rules for efficient graph traversal.

  2. For each mismatched character position, compute shortest paths using Dijkstra's algorithm.

  3. Run Dijkstra from both source characters to find costs to all possible target characters.

  4. Select the common character with minimum combined transformation cost.

  5. Aggregate costs across all mismatched positions to compute total transformation cost.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(n Γ— 26 Γ— E log V), where E is the number of transformation edges and n is string length. Dijkstra runs for each mismatched position with complexity O(E log V) per execution.

  • Auxiliary Space: πŸ’Ύ O(E), for storing the adjacency list representation of the transformation graph.

βœ… Why This Approach?

  • More efficient when transformation graph is sparse with fewer edges.

  • Computes paths on-demand only for characters present in input strings.

  • Avoids unnecessary preprocessing when most transformations are unused.

⚠️ Limitations:

  • Results in Time Limit Exceeded (TLE) for large inputs due to repeated Dijkstra calls.

  • Fails approximately 1010/1120 test cases in competitive environments.

  • Less efficient than Floyd-Warshall for dense transformation graphs.

πŸ†š πŸ” Comparison of Approaches

πŸš€ Approach

⏱️ Time Complexity

πŸ’Ύ Space Complexity

βœ… Pros

⚠️ Cons

🏷️ Floyd-Warshall

🟒 O(26³ + n)

🟒 O(1)

πŸš€ Optimal for dense graphs

πŸ”§ Fixed cubic preprocessing overhead

πŸ” Dijkstra

πŸ”΄ O(n Γ— 26 Γ— E log V)

🟒 O(E)

πŸ“– Good for sparse graphs

⏱️ TLE on large inputs

πŸ† Best Choice Recommendation

🎯 Scenario

πŸŽ–οΈ Recommended Approach

πŸ”₯ Performance Rating

πŸ… Dense transformation graph

πŸ₯‡ Floyd-Warshall

β˜…β˜…β˜…β˜…β˜…

πŸ“– Sparse transformations, small inputs

πŸ₯ˆ Dijkstra

β˜…β˜…β˜…β˜†β˜†

🎯 Competitive programming

πŸ… Floyd-Warshall

β˜…β˜…β˜…β˜…β˜…

β˜• Code (Java)

🐍 Code (Python)

🧠 Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: πŸ“¬ Any Questions?arrow-up-right. Let's make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


πŸ“Visitor Count

Visitor counter

Last updated