Minimum Cost to Convert String I โ€” 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 I
Detailed explanation and reference materials
Problem Overview

๐Ÿ”ค Minimum Cost to Convert String I

Difficulty: Medium
Topic: Graph, Shortest Path


๐Ÿงฉ Problem Statement

You are given two strings:

  • source and target
  • Both are 0-indexed, have the same length, and contain only lowercase English letters

You are also given:

  • original[] โ†’ starting characters
  • changed[] โ†’ resulting characters
  • cost[] โ†’ cost of conversion

๐Ÿ”„ Allowed Operation

In one operation:

  • Pick a character x
  • Change it to y
  • Pay cost z
  • Only allowed if a rule (x โ†’ y, cost z) exists

๐Ÿ“Œ You can apply multiple operations on the same character.


๐ŸŽฏ Objective

Convert source โ†’ target with the minimum total cost.

  • โœ… Return the minimum cost if possible
  • โŒ Return -1 if conversion is impossible

๐Ÿ’ก Key Observations

  • Direct conversion may not be cheapest
  • Characters can be converted using intermediate characters
  • Each character position is independent
  • This can be modeled as a graph shortest-path problem

๐Ÿ“Œ 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]

Explanation

  • b โ†’ c costs 5
  • c โ†’ e costs 1
  • e โ†’ b costs 2
  • d โ†’ e costs 20

Total Cost

5 + 1 + 2 + 20 = 28

โœ… Output: 28


๐Ÿ“Œ Example 2

Input

source  = "aaaa"
target  = "bbbb"

original = ["a","c"]
changed  = ["c","b"]
cost     = [1,2]

Explanation

a โ†’ c โ†’ b
Cost = 1 + 2 = 3 per character
Total = 3 ร— 4 = 12

โœ… Output: 12


๐Ÿ“Œ Example 3

Input

source  = "abcd"
target  = "abce"

original = ["a"]
changed  = ["e"]
cost     = [10000]

Explanation

  • No conversion exists for d โ†’ e

โŒ Output: -1


๐Ÿง  Conceptual Model

  • Each character (aโ€“z) is a node
  • Each conversion is a directed edge
  • Edge weight = conversion cost
  • Goal: Find the minimum cost path between characters

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

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

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.