EasyHash TableStringUpdated Sep 2026

205. Isomorphic Strings

Asked at Bloomberg

Difficulty

Easy

Time

O(n)

Space

O(k)

Pattern

Hash Table

Companies

1

Problem

You are given two strings, s and t, of the same length. Decide whether they are isomorphic.

Two strings are isomorphic when you can rename the characters of s to get t. The rename rule must be consistent: every copy of a character in s becomes the same character in t, and the order of characters never changes. A character may map to itself.

The mapping must also be one-to-one. Two different characters in s cannot both map to the same character in t. Return true if such a mapping exists, otherwise false.

Rename characters of s to get t

segg012The two 'g' positions must map to the same targetcharacter.tadd012e -> a and g -> d is consistent, so the answer istrue.
Each character in s must always become the same character in t, and no two source characters may share a target.

Constraints

  • ●1 <= s.length <= 5 * 10^4
  • ●t.length == s.length
  • ●s and t consist of any valid ASCII character

Examples

Example 1
Inputs = "egg", t = "add"
Outputtrue
WhyMap 'e' to 'a' and 'g' to 'd'. Both 'g' positions become 'd', so the rename is consistent and no two source characters share a target.
Example 2
Inputs = "f11", t = "b23"
Outputfalse
WhyThe character '1' appears at positions 1 and 2 in s, but those positions hold '2' and '3' in t. One source character would need two different targets, which is not allowed.
Example 3
Inputs = "paper", t = "title"
Outputtrue
WhyThe mapping p->t, a->i, e->l, r->e works. The repeated 'p' always lands on 't', and every target character is used by only one source character.

Asked At

1 companies
CompanyDifficulty
BloombergEasyView all Bloomberg questions →

How to Think About It

1

A single map from s to t is not enough. For s = "ab", t = "aa", the map a->a, b->a is consistent in one direction but two source characters share a target.

2

Key insight: keep two maps — s -> t and t -> s — and check both at every position. If either existing mapping disagrees with the current pair, return false.

3

An equivalent trick: compare the "first index" pattern. Replace each character by the index where it first appeared; "egg" becomes [0,1,1] and "add" becomes [0,1,1]. The strings are isomorphic iff their patterns match.

4

Walkthrough for s = "paper", t = "title": p->t, a->i, p->t (consistent), e->l, r->e. Reverse map has no conflicts -> true.

5

Edge cases: the strings are guaranteed equal length. Characters can be any ASCII, including digits and spaces.

Optimal Approach

Step 1: Create maps st and ts.
Step 2: For each position i with a = s[i], b = t[i]:
If a in st and st[a] != b: return false.
If b in ts and ts[b] != a: return false.
Set st[a] = b and ts[b] = a.
Step 3: Return true.

Time: O(n). Space: O(k) where k is the alphabet size.

Two maps, checked at every position

spaper01234iStart at position 0: p and t are new, so record p->tand t->p.ttitle01234iAt position 2 the pair is (p, t) again. Both mapsalready say p<->t, so it is fine.st[a] must equal bts[b] must equal aelse return false
Keep s->t and t->s. If either map already disagrees with the current pair, the strings are not isomorphic.
ComplexityTime O(n)Space O(k)We scan the two strings once, and the two hash maps hold at most one entry per distinct character, where k is the alphabet size.

What Trips People Up in Real Interviews

1

Checking only one direction. "badc" vs "baba" passes an s -> t check but fails because b and d both map to b.

2

Sorting or counting characters. Frequency counts cannot capture order — "ab" vs "ba" have the same counts and are isomorphic, but "aab" vs "aba" have the same counts and are not.

3

Assuming only lowercase letters. The input can contain any printable ASCII, so use a map (or a 256-size array), not a 26-size array.

4

Building the full mapping first and validating later. Checking at each step lets you exit early and keeps the code short.

Solution Code

O(n) time
def isIsomorphic(s, t):
    st, ts = {}, {}
    for a, b in zip(s, t):
        if st.get(a, b) != b or ts.get(b, a) != a:
            return False
        st[a] = b
        ts[b] = a
    return True

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Isomorphic Strings problem?

Isomorphic Strings asks whether the characters of `s` can be replaced to get `t`, where every occurrence of a character maps to the same replacement and no two different characters map to the same one. `"egg"` and `"add"` are isomorphic; `"foo"` and `"bar"` are not. The catch is that the mapping must be one-to-one in both directions.

How do you solve Isomorphic Strings?

The optimal approach runs in O(n) time and O(k) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Isomorphic Strings?

Isomorphic Strings is asked at Bloomberg. It is a easy difficulty problem.

What are common mistakes on Isomorphic Strings?
  • Checking only one direction. `"badc"` vs `"baba"` passes an `s -> t` check but fails because `b` and `d` both map to `b`.
  • Sorting or counting characters. Frequency counts cannot capture order — `"ab"` vs `"ba"` have the same counts and are isomorphic, but `"aab"` vs `"aba"` have the same counts and are not.
  • Assuming only lowercase letters. The input can contain any printable ASCII, so use a map (or a 256-size array), not a 26-size array.
  • Building the full mapping first and validating later. Checking at each step lets you exit early and keeps the code short.