28. Isomorphic Strings
easyAsked at VercelDetermine if two strings are isomorphic (consistent one-to-one character mapping). Vercel asks this because the bidirectional-mapping check is the same shape as their canary deploy 'this version maps consistently to that traffic slice' validator.
By Sam K., Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Vercel loops.
- Glassdoor (2025-12)— Vercel screen; bidirectional map expected.
- LeetCode Discuss (2026-Q1)— Listed in Vercel interview prep notes.
Problem
Given two strings s and t, determine if they are isomorphic. Two strings s and t are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.
Constraints
1 <= s.length <= 5 * 10^4t.length == s.lengths and t consist of any valid ASCII characters.
Examples
Example 1
s = "egg", t = "add"trueExample 2
s = "foo", t = "bar"falseExample 3
s = "paper", t = "title"trueApproaches
1. Single-direction mapping
Map each s[i] -> t[i]; if it ever conflicts, return false.
- Time
- O(n)
- Space
- O(1) for ASCII
// WRONG — only checks one direction.
function isIsomorphic(s, t) {
const map = {};
for (let i = 0; i < s.length; i++) {
if (map[s[i]] && map[s[i]] !== t[i]) return false;
map[s[i]] = t[i];
}
return true;
}
// Fails for s='ab', t='aa' — 'a' and 'b' both map to 'a'.Tradeoff: Misses the 'no two source characters map to the same target' rule. Vercel will catch this.
2. Bidirectional mapping (optimal)
Two maps: s[i] -> t[i] and t[i] -> s[i]. Both must be consistent.
- Time
- O(n)
- Space
- O(1) for ASCII
function isIsomorphic(s, t) {
if (s.length !== t.length) return false;
const sToT = {};
const tToS = {};
for (let i = 0; i < s.length; i++) {
const a = s[i], b = t[i];
if (sToT[a] && sToT[a] !== b) return false;
if (tToS[b] && tToS[b] !== a) return false;
sToT[a] = b;
tToS[b] = a;
}
return true;
}Tradeoff: Two maps catch both directions of the 1-1 constraint. O(1) extra space because ASCII is bounded.
Vercel-specific tips
Vercel grades the bidirectional check. Bonus signal: noting that 'isomorphic' implies a bijection between the character sets used, and that single-direction mapping is the most common bug. They may follow up by asking 'what if it's a homomorphism instead of isomorphism?' to test definitions.
Common mistakes
- Only checking s -> t — fails for ('ab', 'aa').
- Using `if (map[s[i]])` and missing the case where the mapped char is the empty string (won't happen in ASCII but flag it).
- Initializing the map as `new Map()` and forgetting Map.has() vs key lookup.
Follow-up questions
An interviewer at Vercel may pivot to one of these next:
- Word Pattern (LC 290) — same shape with words instead of characters.
- Generalize to k-isomorphism (allow up to k mismatches).
- Find the largest isomorphic substring.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why two maps and not one?
Isomorphism requires a bijection. One map only enforces 'each s character maps to one t character.' The second map enforces 'each t character is mapped FROM at most one s character' — the 1-1 part.
Why is space O(1)?
The alphabet is ASCII (256 characters). The maps have at most 256 entries each, independent of n.