DSA AnimatorDSA animations
Isomorphic Strings LC #205 Easy Bijection Map
Problem

Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t, where each character maps to exactly one other character and preserving order. No two characters may map to the same character.

Example 1
Input: s = "egg", t = "add"
Output: true
Explanation: e→a, g→d — consistent bijection.
Example 2
Input: s = "foo", t = "bar"
Output: false
Explanation: o maps to both a and r — conflict at index 2.
Constraints: 1 ≤ s.length ≤ 5×10⁴ | t.length == s.length | s and t consist of any valid ASCII character
Try Examples
Custom:
Approach
Two maps: sToT and tToS — enforce bijection in both directions
At each position, check both maps for conflicts. One map alone is insufficient (e.g. "ab"→"aa" would falsely pass).
Character Comparison
s
t
—
↔
—
—
s→t mapping (sToT)
empty
t→s mapping (tToS)
empty
Variables
i
—
sc
—
tc
—
sToT
—
res
—
Step Logic
Press ▶ Play or Next Step to begin the animation.
🎉
Ready
0 / 0
Select an example above and press Play.
Algorithm
1
Init sToT = {} and tToS = {} (bijection maps)
2
For each i: get sc = s[i], tc = t[i]
3
If sToT[sc] exists and sToT[sc] ≠ tc → return false
4
If tToS[tc] exists and tToS[tc] ≠ sc → return false
5
Set sToT[sc] = tc and tToS[tc] = sc
6
No conflicts after full scan → return true
Time
O(n)
Space
O(1)
Why This Works

Isomorphic = one-to-one character mapping (bijection). A single map is insufficient — e.g. "ab"→"aa" would pass a one-direction check since both a and b map to a. Two maps enforce both directions: every s-char maps to exactly one t-char and every t-char maps back to exactly one s-char.

How to solve Isomorphic Strings

LeetCode 205 · Easy · Strings · Pattern: Two Hash Maps

Why the obvious approach is slow

Checking only one direction is the common mistake. For badc and baba, b maps to b and a to a, but then d would also map to b, which must not happen.

The key idea

Keep two maps: one from the first string to the second, and one from the second back to the first. At each position, the pair must agree with both maps, or be new to both.

Remember

Isomorphic means a one-to-one mapping, so check both directions. Two maps, one for each direction.

Run the animation above on your own input, or watch the 1:24 video explanation of Isomorphic Strings with a dry run and Java code.

More Strings problems