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.

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

Visualization

Tap to expand
Minimum Cost to Convert String II INPUT Source: a b c d Target: a c c e Transformation Rules: "a" --> "a" cost: 1 "b" --> "c" cost: 2 "c" --> "b" cost: 3 "cc" --> "e" cost: 4 "d" --> "e" cost: 5 ALGORITHM (DP) 1 Build Cost Graph Floyd-Warshall for shortest paths 2 DP State Setup dp[i] = min cost to convert source[0..i] 3 Try All Substrings For each position, try all valid rules 4 Minimize Cost dp[i+len] = min of dp[i] + rule cost DP Table: 0 0 2 6 7 idx: 0 1 2 3 4 FINAL RESULT Optimal Path: a OK (0) b-->c cost: 2 c-->c OK (0) d-->e cost: 5 Cost Breakdown: Position 0: a=a (same) 0 Position 1: b-->c 2 Position 2: c=c (same) 0 Position 3: d-->e 5 Total Cost: 7 OUTPUT 7 Minimum cost: 7 Key Insight: Use Floyd-Warshall to precompute minimum costs between all substring pairs, then apply DP where dp[i] represents the minimum cost to transform source[0..i-1] to target[0..i-1]. Try all valid transformation rules at each position and take the minimum total cost. TutorialsPoint - Minimum Cost to Convert String II | Dynamic Programming Approach
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