Isomorphic Strings — 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 Isomorphic Strings
Detailed explanation and reference materials
Problem Overview

Character Replacement to Match Strings

Problem Statement:
Given two strings s and t of the same length, determine whether it is possible to replace characters in s such that it becomes identical to t. Each character in s can be replaced with at most one unique character from t, and no two characters in s can map to the same character in t.


Approach

To solve this problem, we use two dictionaries (dic1 and dic2) to map characters from s to t and t to s respectively. These mappings help ensure one-to-one correspondence between characters.

Steps:

  1. Initialize Variables:

    • Two dictionaries, dic1 and dic2, to track mappings.
    • A flag variable set to 1, indicating success.
  2. Iterate Through Both Strings Simultaneously:

    • For each character pair (s[i], t[i]):
      • If s[i] is already in dic1 and maps to a different character than t[i], or if t[i] is in dic2 and maps to a different character than s[i], set flag = 0 and break.
      • Otherwise, add the mapping s[i] -> t[i] in dic1 and t[i] -> s[i] in dic2.
  3. Final Check:

    • If flag is still 1 after processing all characters, return True.
    • Otherwise, return False.

Code Implementation

python
def canTransform(s, t):
    if len(s) != len(t):
        return False
    
    dic1 = {}
    dic2 = {}
    flag = 1
    
    for i in range(len(s)):
        if (s[i] in dic1 and dic1[s[i]] != t[i]) or (t[i] in dic2 and dic2[t[i]] != s[i]):
            flag = 0
            break
        dic1[s[i]] = t[i]
        dic2[t[i]] = s[i]
    
    return flag == 1
---

**Input:**  
`s = "abc", t = "def"`

**Execution:**  
- `dic1`: `{'a': 'd', 'b': 'e', 'c': 'f'}`  
- `dic2`: `{'d': 'a', 'e': 'b', 'f': 'c'}`  

**Output:**  
`True`

---

**Input:**  
`s = "foo", t = "bar"`

**Execution:**  
- Mapping `o -> a` is inconsistent.  

**Output:**  
`False`

---

**Input:**  
`s = "add", t = "egg"`

**Execution:**  
- `dic1`: `{'a': 'e', 'd': 'g'}`  
- `dic2`: `{'e': 'a', 'g': 'd'}`  

**Output:**  
`True`

---
**Input:**  
`s = "abc", t = "de"`

**Execution:**  
- Since the lengths are different, no transformation is possible.  

**Output:**  
`False`

---

**Input:**  
`s = "", t = ""`

**Execution:**  
- Both strings are empty, so they are trivially the same.  

**Output:**  
`True`

— Written by Saurabh Patil • B.Tech CSE • Software Developer

Categories
heaps-&-hashing
java
Reference Link
https://leetcode.com/problems/isomorphic-strings

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.

EmailLinkedInTwitterInstagramGitHub
© 2026 DrawToCode. All rights reserved.