← Back to DSA Animator
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
st mapping (sToT)
empty
ts 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] ≠ tcreturn false
4
If tToS[tc] exists and tToS[tc] ≠ screturn 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.