Minimum Cost to Convert String I - Problem

You are given two 0-indexed strings source and target, both of length n and consisting of lowercase English letters. You are also given two 0-indexed character arrays original and changed, and an integer array cost, where cost[i] represents the cost of changing the character original[i] to the character changed[i].

You start with the string source. In one operation, you can pick a character x from the string and change it to the character y at a cost of z if there exists any index j such that cost[j] == z, original[j] == x, and changed[j] == y.

Return the minimum cost to convert the string source to the string target using any number of operations. If it is impossible to convert source to target, return -1.

Note that there may exist indices i, j such that original[j] == original[i] and changed[j] == changed[i].

Input & Output

Example 1 — Basic Conversion
$ Input: source = "abcd", target = "acbe", original = ["a","b","c","c","e","d"], changed = ["b","c","b","e","b","e"], cost = [2,5,5,1,2,20]
Output: 28
💡 Note: Convert 'b'→'c' at position 1 (cost 5), 'd'→'e' at position 3 (cost 20), and 'd'→'b' at position 3. Wait, we need to be more careful. Position 1: b→c (cost 5), Position 3: d→e (cost 20). Total cost = 5 + 20 = 25. Actually checking again: pos 1 b→c cost 5, pos 3 d needs to become e. Looking at conversions, d→e costs 20. But we can do d→c→e which might be cheaper. Let's see: no direct d→c. So d→e costs 20. But wait, we can use d→e directly. Actually, let me recalculate: pos 1: b→c (from original[1]→changed[1], cost[1]=5), pos 3: d→e (cost 20 from original[5]→changed[5], cost[5]=20). But there might be indirect paths. This needs Floyd-Warshall to find optimal.
Example 2 — Impossible Conversion
$ Input: source = "aaaa", target = "bbbb", original = ["a","c"], changed = ["c","b"], cost = [1,2]
Output: -1
💡 Note: We can convert a→c (cost 1) and c→b (cost 2), so a→c→b costs 3 total. Each 'a' needs to become 'b', total cost would be 4 × 3 = 12. Wait, that's possible. Let me reconsider... Actually this should return 12, not -1.
Example 3 — No Conversion Needed
$ Input: source = "abcd", target = "abcd", original = ["a"], changed = ["b"], cost = [5]
Output: 0
💡 Note: Source and target are identical, no conversions needed, cost is 0

Constraints

  • 1 ≤ source.length == target.length ≤ 105
  • source, target consist of lowercase English letters
  • 1 ≤ original.length == changed.length == cost.length ≤ 2000
  • original[i], changed[i] are lowercase English letters
  • 1 ≤ cost[i] ≤ 106

Visualization

Tap to expand
Minimum Cost String Conversionsource: "abc"target: "adc"Character conversions needed:b → d (pos 1)c → c (pos 2)Build graph of character conversion costsFloyd-Warshall AlgorithmPrecompute shortest paths between all character pairsHandle indirect conversions: a→x→y→bOutput: Minimum total conversion cost
Understanding the Visualization
1
Input
Source string, target string, and conversion rules with costs
2
Process
Find minimum cost path for each character that needs conversion
3
Output
Total minimum cost or -1 if impossible
Key Takeaway
🎯 Key Insight: This is a shortest path problem where characters are nodes and conversions are weighted edges
Asked in
Google 15 Meta 12 Amazon 8
12.4K Views
Medium Frequency
~25 min Avg. Time
456 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