29. Isomorphic Strings
easyAsked at RedditCheck if two strings are isomorphic (each character in s can be replaced to get t). Reddit asks this to test bidirectional mapping — the same skill needed for their username-to-ID isomorphism guarantees in cross-shard joins.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Reddit loops.
- Glassdoor (2026-Q1)— Reddit phone screen, common dictionary warm-up.
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 character.
Examples
Example 1
s = "egg", t = "add"trueExample 2
s = "foo", t = "bar"falseExample 3
s = "paper", t = "title"trueApproaches
1. Single map (one direction)
Map s[i] -> t[i]. Check existing mappings.
- Time
- O(n)
- Space
- O(1)
// WRONG: 'foo' -> 'bar': f->b, o->a, o->r. Caught.
// But 'ab' -> 'aa': a->a, b->a. Same target — needs second map.Tradeoff: Misses the 'no two chars map to the same target' constraint.
2. Bidirectional maps (optimal)
Two maps: s->t and t->s. Walk both strings; consistent mappings in both directions.
- Time
- O(n)
- Space
- O(1)
function isIsomorphic(s, t) {
if (s.length !== t.length) return false;
const sToT = new Map(), tToS = new Map();
for (let i = 0; i < s.length; i++) {
const a = s[i], b = t[i];
if (sToT.has(a)) {
if (sToT.get(a) !== b) return false;
} else if (tToS.has(b)) {
if (tToS.get(b) !== a) return false;
} else {
sToT.set(a, b);
tToS.set(b, a);
}
}
return true;
}Tradeoff: O(1) space (max 256 ASCII chars). The bidirectional check is the entire trick.
Reddit-specific tips
Reddit interviewers grade on whether you remember the no-merge constraint. Bonus signal: connect to their schema-evolution policy — column renames must be bijective during migration, never many-to-one.
Common mistakes
- Using only one map (misses many-to-one).
- Comparing strings directly (s.replace) — quadratic.
- Forgetting to check both directions on every char.
Follow-up questions
An interviewer at Reddit may pivot to one of these next:
- Word pattern (LC 290) — same bidirectional pattern.
- Group anagrams (LC 49) — different structure.
- What if the alphabet is unicode?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why two maps instead of one + a reverse-lookup set?
Equivalent. Set of taken values + map source->target works too. Two maps reads cleaner.
Why is this O(1) space?
At most 256 unique ASCII chars, so each map is bounded by 256 entries — constant w.r.t. n.
Practice these live with InterviewChamp.AI
Drill Isomorphic Strings and other Reddit interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →