Minimum Cost to Convert String II - Problem
Minimum Cost to Convert String II

You're given two strings source and target of equal length, along with transformation rules. Each rule specifies that you can convert a substring original[i] to changed[i] at a cost of cost[i].

The challenge is to find the minimum cost to transform the entire source string into the target string using these substring replacement operations.

Key Rules:
• Operations on substrings must either be disjoint (non-overlapping) or identical (same positions)
• You can use any transformation rule multiple times
• If transformation is impossible, return -1

Example: If source = "abcd" and target = "acce", and you have rules to convert "b" → "c" (cost 2) and "d" → "e" (cost 3), the minimum cost would be 5.

Input & Output

example_1.py — Basic transformation
$ Input: source = "abcd", target = "acce", original = ["a","b","c","cc","d"], changed = ["a","c","b","e","e"], cost = [1,2,3,4,5]
Output: 7
💡 Note: We can transform 'b' to 'c' (cost 2) and 'd' to 'e' (cost 5). Total cost is 2 + 5 = 7. The character 'a' stays the same and 'c' becomes 'c' with no cost.
example_2.py — Impossible transformation
$ Input: source = "abcd", target = "abce", original = ["a"], changed = ["e"], cost = [10000]
Output: -1
💡 Note: We need to transform 'd' to 'e', but there's no rule for this transformation. The only rule transforms 'a' to 'e', which doesn't help us.
example_3.py — Multi-character transformations
$ Input: source = "abcdef", target = "axydef", original = ["bc","x","y"], changed = ["xy","a","b"], cost = [5,3,2]
Output: 5
💡 Note: We can directly transform the substring 'bc' to 'xy' with cost 5. This is more efficient than trying to transform individual characters.

Visualization

Tap to expand
String Transformation VisualizationSource:abcdTarget:accecost=2cost=5Transformation Graph:bcde25DP Table (bottom-up):dp[4]dp[3]dp[2]dp[1]dp[0]05027🎯 Key Insight: Precompute all shortest transformation paths, then use DP for optimal segmentationTime: O(n³ + n²m) | Space: O(n² + k²) where k = unique substrings, m = avg substring length
Understanding the Visualization
1
Build transformation graph
Each unique substring becomes a node, transformation rules become weighted edges
2
Find shortest paths
Use Floyd-Warshall to find cheapest way to transform between any two substrings
3
Segment optimally
Use DP to find the best way to divide the string into transformable segments
4
Combine costs
Each segment uses the precomputed shortest transformation cost
Key Takeaway
🎯 Key Insight: Transform the substring replacement problem into a shortest path problem on a graph, then use dynamic programming to find the optimal segmentation. This combines graph algorithms (Floyd-Warshall) with DP for an elegant and efficient solution.

Time & Space Complexity

Time Complexity
⏱️
O(n³ + n²×m)

O(n³) for Floyd-Warshall on substring graph, O(n²×m) for DP where m is average substring length

n
2n
Quadratic Growth
Space Complexity
O(n² + k²)

O(n²) for DP table, O(k²) for shortest path matrix where k is number of unique substrings

n
2n
Quadratic Space

Constraints

  • 1 ≤ source.length == target.length ≤ 103
  • source, target consist only of lowercase English letters
  • 1 ≤ original.length == changed.length == cost.length ≤ 100
  • 1 ≤ original[i].length == changed[i].length ≤ source.length
  • original[i], changed[i] consist only of lowercase English letters
  • 1 ≤ cost[i] ≤ 106
  • Operations must be on disjoint substrings or identical positions
Asked in
Google 35 Meta 28 Amazon 22 Microsoft 18
31.5K Views
Medium Frequency
~35 min Avg. Time
1.3K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen