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: 25
💡 Note: We need to convert position 1: b→c (cost 5) and position 3: d→e (cost 20). The Floyd-Warshall algorithm finds these are the optimal paths. Total cost = 5 + 20 = 25.
Example 2 — Impossible Conversion
$ Input: source = "aaaa", target = "bbbb", original = ["a","c"], changed = ["c","b"], cost = [1,2]
Output: 12
💡 Note: We can convert a→c (cost 1) then c→b (cost 2), so each 'a' converts to 'b' with total cost 3. Since we need to convert 4 characters, total cost is 4 × 3 = 12.
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 to Convert String I INPUT source: "abcd" target: "acbe" Transformation Graph a b c d e 2 5 1 5 2 20 original: [a,b,c,c,e,d] changed: [b,c,b,e,b,e] cost: [2,5,5,1,2,20] Position Differences: a=a b-c c-b d-e ALGORITHM STEPS 1 Build Cost Matrix 26x26 matrix for all chars 2 Initialize Direct Costs dist[i][i]=0, direct edges 3 Floyd-Warshall Find all shortest paths Shortest Path Matrix (subset) b c e b [ 0 5 7 ] c [ 5 0 1 ] d [ 22 27 8 ] 4 Sum Conversion Costs For each differing position pos 0: a--a = 0 (same) pos 1: b--c = 5 pos 2: c--b = 5 pos 3: d--e = 8 (via b) FINAL RESULT Optimal Conversion Path source: "abcd" target: "acbe" Cost Breakdown: Position 0: a to a = 0 Position 1: b to c = 5 Position 2: c to b = 5 Position 3: d to e (d--b--c--e: 20+5+1=26?) Best: d--e direct = 20 Wait: d to e via shortest = 8 Total Cost 28 0 + 5 + 5 + 18 = 28 Key Insight: Floyd-Warshall computes ALL shortest paths between character pairs in O(26^3) = O(1). This allows finding minimum cost for ANY character transformation, even through intermediate chars. Example: d to e might be cheaper via d--b--c--e than direct d--e edge! TutorialsPoint - Minimum Cost to Convert String I | Floyd-Warshall All-Pairs Shortest Path
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