Isomorphic Strings
An easy Strings interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Strings
- Sheets
- 1
- Core for
- 14 roles
The problem
Determine whether two strings are isomorphic, meaning each character in the first string can be replaced to get the second string while preserving the order of characters.
Example 1
- Input
- s="egg", t="add"
- Output
- true
Example 2
- Input
- s="foo", t="bar"
- Output
- false
Example 3
- Input
- s="paper", t="title"
- Output
- true
Constraints
- 1 <= s.length <= 5*10^4
- s and t have the same length
How to think about it
Updated 2026-09-29Isomorphism requires a strict one-to-one bijection between characters in both directions. Recording a mapping from s to t is only half the check; multiple distinct characters in s must not collapse into the same character in t. Tracking the last seen position of each character in both strings exposes any discrepancy immediately.
Approaches, worst first
Two hash maps bijection
time O(n) · space O(1)
Maintain mapST to store s[i] -> t[i] and mapTS to store t[i] -> s[i]. At each index i, if either mapping exists and differs from the current character pair, return false; otherwise register both assignments.
Last-seen index arraysWrite this one
time O(n) · space O(1)
Maintain two integer arrays of size 256 initialized to zero, recording the 1-based index where each character was last encountered. At each step, if lastSeenS[s[i]] != lastSeenT[t[i]], the structural alignment is broken. Otherwise, update both to i + 1.
Where people lose marks · 3
- Only mapping s -> t: for 'badc' and 'baba', 'd' and 'c' both map to 'a', violating the bijection if reverse mapping t -> s is omitted.
- Characters span ASCII: ensure indexing supports values up to 256 rather than assuming only lowercase letters.
- Using 0 as both the default unvisited marker and valid index 0; use 1-based indexing (i + 1) to distinguish unvisited characters from characters seen at position 0.
The theory behind it
Strings — the ground this problem stands on. All Strings problems
What Strings is
A string is an ordered necklace of text characters, like letters printed along a ribbon of paper. Each character sits at an exact numeric slot, holding a glyph such as a letter, punctuation mark, or digit. In many programming languages, ribbons cannot be edited after creation, meaning changing a single character requires pressing an entirely new ribbon from scratch.
When to reach for it
Reach for string techniques when inputs consist of words, DNA sequences, serialized data formats, or sentences. Clues include questions testing palindromes, anagram matches, substring patterns, parenthesis balancing, or character frequency counts. Whenever an algorithm asks to transform capitalization, parse structured tokens, or compute edits between two phrases, string representations are the core subject.
How the pattern works
Think of characters as small integer codes ranging across standard character sets. Frequency tables with fixed sizes often replace heavy hash maps when tallying occurrences. For search tasks, maintain rolling state using character indices or sliding borders. When building output text through repeated appends, accumulate pieces inside a mutable list or string builder rather than concatenating strings directly, avoiding quadratic copy overhead.
More Strings problems
Target Roles
Core requirement for 14 roles:
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .