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
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.
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.
Initialize Variables:
dic1 and dic2, to track mappings.flag variable set to 1, indicating success.Iterate Through Both Strings Simultaneously:
(s[i], t[i]):
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.s[i] -> t[i] in dic1 and t[i] -> s[i] in dic2.Final Check:
flag is still 1 after processing all characters, return True.False.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`
🎯 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.