Minimum Cost to Convert String II โ€” Algorithm Visualization & Coding Challenge

Choose Your Learning Path

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.

๐Ÿง  Active Learning

Visualize the algorithm step-by-step with interactive animations in real time.

๐Ÿ“– Passive Learning

Read the full explanation, examples, and starter code at your own pace.

๐ŸŽฏ Challenge Mode

Drag and arrange the algorithm steps in the correct execution order.

๐Ÿง  Select Active to activate

JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time.

๐Ÿ“– Select Passive to activate

Understanding Minimum Cost to Convert String II
Detailed explanation and reference materials
Problem Overview

Minimum Cost to Convert String II

Difficulty: Hard
Topics: Graph, DP, Strings


๐Ÿงฉ Problem Statement

You are given two 0-indexed strings source and target, both of length n, consisting of lowercase English characters.

You are also given:

  • Two string arrays: original and changed
  • An integer array cost

Where:

  • cost[i] represents the cost of converting original[i] โ†’ changed[i]

๐Ÿ” Operation Rules

You start with the string source.

In one operation, you can:

  • Pick a substring x
  • Convert it to y at cost z
  • If there exists an index j such that:
    • original[j] === x
    • changed[j] === y
    • cost[j] === z

You may perform any number of operations, but any pair of operations must satisfy one of the following:

  1. Disjoint Substrings

    • Operations on source[a..b] and source[c..d]
    • Valid only if b < c or d < a
  2. Same Substring (Repeated Transformation)

    • Operations on source[a..b] and source[c..d]
    • Valid only if a === c AND b === d

๐ŸŽฏ Goal

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] and changed[i] === changed[j]


๐Ÿ“Œ Examples


โœ… Example 1

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

  • Change "b" โ†’ "c" at index 1 (cost 5)
  • Change "c" โ†’ "e" at index 2 (cost 1)
  • Change "e" โ†’ "b" at index 2 (cost 2)
  • Change "d" โ†’ "e" at index 3 (cost 20)

Total Cost:
5 + 1 + 2 + 20 = 28


โœ… Example 2

Input

source = "abcdefgh"
target = "acdeeghh"
original = ["bcd","fgh","thh"]
changed  = ["cde","thh","ghh"]
cost     = [1,3,5]

Output

9

Explanation

  • Convert "bcd" โ†’ "cde" (indices 1โ€“3, cost 1)
  • Convert "fgh" โ†’ "thh" (indices 5โ€“7, cost 3)
  • Convert "thh" โ†’ "ghh" (same indices, cost 5)

Total Cost:
1 + 3 + 5 = 9


โŒ Example 3

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.


๐Ÿ“ Constraints

  • 1 โ‰ค source.length = target.length โ‰ค 1000
  • 1 โ‰ค original.length = changed.length = cost.length โ‰ค 100
  • 1 โ‰ค original[i].length = changed[i].length โ‰ค source.length
  • All strings contain only lowercase English letters
  • original[i] โ‰  changed[i]
  • 1 โ‰ค cost[i] โ‰ค 10^6

โ€” Written by Saurabh Patil โ€ข B.Tech CSE โ€ข Software Developer

Categories
graphs
dp
strings
leetcode-problem-of-the-day
java
Reference Link
https://leetcode.com/problems/minimum-cost-to-convert-string-ii/

Loading component...

Starter Code
Test, modify, or copy the starter code. Click "Visualize" to import into the canvas.
Java
Output:
Understood Algorithm, Test Me now ๐ŸŽฎ

๐ŸŽฏ Select Challenge to activate

๐Ÿง  Logic Puzzle
Think & Arrange, Don't Just Copy-Paste

Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.

Arrange the Algorithm Correctly ๐Ÿงฉ

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 Algorithm

Don't Know Current Algorithm ? ย 

Green text means the instruction is placed in the correct position.

Red text means the instruction is in the wrong position.

Block Colors

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.

DrawToCode โ€” Visualize, Practice & Master Algorithms

Learn data structures and algorithms through interactive visualizations. Practice coding problems, track your progress, and understand concepts deeply.

Visualization Boardย |ย Problemsย |ย Aboutย |ย Privacy Policy
EmailLinkedInTwitterInstagramGitHub
ยฉ 2026 DrawToCode. All rights reserved.