How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focusโor scroll down to preview all options.
Visualize the algorithm step-by-step with interactive animations in real time.
Read the full explanation, examples, and starter code at your own pace.
Drag and arrange the algorithm steps in the correct execution order.
๐ง Select Active to activate
Follow every state change, comparison, and transformation as the execution unfolds in real time.
๐ Select Passive to activate
Difficulty: Hard
Topics: Graph, DP, Strings
You are given two 0-indexed strings source and target, both of length n, consisting of lowercase English characters.
You are also given:
original and changedcostWhere:
cost[i] represents the cost of converting original[i] โ changed[i]You start with the string source.
In one operation, you can:
j such that:
original[j] === xchanged[j] === ycost[j] === zYou may perform any number of operations, but any pair of operations must satisfy one of the following:
Disjoint Substrings
source[a..b] and source[c..d]Same Substring (Repeated Transformation)
source[a..b] and source[c..d]Return the minimum cost to convert source into target.
If it is impossible, return -1.
โ ๏ธ Note:
Multiple indices may exist such that
original[i] === original[j]andchanged[i] === changed[j]
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
Explanation
"b" โ "c" at index 1 (cost 5)"c" โ "e" at index 2 (cost 1)"e" โ "b" at index 2 (cost 2)"d" โ "e" at index 3 (cost 20)Total Cost:
5 + 1 + 2 + 20 = 28
Input
source = "abcdefgh"
target = "acdeeghh"
original = ["bcd","fgh","thh"]
changed = ["cde","thh","ghh"]
cost = [1,3,5]
Output
9
Explanation
"bcd" โ "cde" (indices 1โ3, cost 1)"fgh" โ "thh" (indices 5โ7, cost 3)"thh" โ "ghh" (same indices, cost 5)Total Cost:
1 + 3 + 5 = 9
Input
source = "abcdefgh"
target = "addddddd"
original = ["bcd","defgh"]
changed = ["ddd","ddddd"]
cost = [100,1578]
Output
-1
Explanation It is impossible to convert the string due to overlapping substring constraints.
1 โค source.length = target.length โค 10001 โค original.length = changed.length = cost.length โค 1001 โค original[i].length = changed[i].length โค source.lengthoriginal[i] โ changed[i]1 โค cost[i] โค 10^6๐ฏ Select Challenge to activate
Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.
The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.
Understand Below AlgorithmGreen text means the instruction is placed in the correct position.
Red text means the instruction is in the wrong position.
Instructions with the same background color indicate particular blocks start and end.
A tick mark means the instruction is correct and locked.
๐ Locked steps cannot be moved. Only unlocked steps are draggable.
๐ Enable sound for swap feedback and completion effects.