Skip to content

Interview Solver

FeaturesPricingHelp
Sign In
FeaturesPricingHelpSign In
  1. Interview Questions
  2. /
  3. Problems
  4. /
  5. Isomorphic Strings

Isomorphic Strings

Decide whether one string can be turned into another by consistently renaming its characters. The catch is that the renaming has to work in both directions, so you need two maps or an equivalent trick. Solve it below in Python or JavaScript, then read the walkthrough.

Solve it in your browserRead the solution
EasyHash TableStringAsked at 82 companies incl. Google, Amazon, Meta

Problem

You're given two strings s and t of the same length. They are isomorphic if you can replace every character of s with some character to get t, following these rules:

  • Every occurrence of a given character must be replaced by the same character.
  • Two different characters may not be replaced by the same character.
  • A character may be replaced by itself.

In other words, there must be a one-to-one mapping from the characters of s to the characters of t that turns s into t position by position. Return true if such a mapping exists and false otherwise.

Example 1

Input
s = "noon", t = "abba"
Output
true

Map n → a and o → b.

Example 2

Input
s = "abcd", t = "xyyz"
Output
false

b and c would both have to map to y, which is not allowed.

Example 3

Input
s = "aab", t = "xyz"
Output
false

The two a characters would have to map to both x and y.

Constraints

  • 1 ≤ s.length ≤ 5 × 10⁴
  • t.length == s.length
  • s and t consist of printable ASCII characters.

Practice

1
2
3
4

Runs 15 tests in your browser. Python loads on the first run (about 6 MB, then cached). Press ⌘/Ctrl + Enter to run.

Get asked Isomorphic Strings in a real interview?

Interview Solver gives you a working solution live, during your actual coding interview. Invisible to screensharing.

Start Free Trial

How to solve Isomorphic Strings

Pattern: Bijection check with two hash maps

Walk both strings together and record the mapping s[i] → t[i] as you go. If a character of s was already mapped to something else, the mapping isn't consistent, so return false.

That alone isn't enough. With s = "ab" and t = "aa", the forward map a → a, b → a is perfectly consistent, but two different characters land on the same target. You also have to check the reverse direction: each character of t must come from only one character of s. Two maps, one each way, catch both kinds of conflict.

An equivalent view: two strings are isomorphic exactly when every position has the same "first seen at" pattern. Record, for each character, the index where it first appeared, and compare the two sequences.

Approach 1: Compare every pair of positions

For every pair i < j, check that s[i] == s[j] exactly when t[i] == t[j]. This captures the definition directly but is quadratic.

Time O(n²) · Space O(1)

Approach 2: First-occurrence signatures

Replace each character with the index of its first occurrence in its own string, so "noon" becomes [0, 1, 1, 0]. The strings are isomorphic exactly when the two sequences are equal. Linear and neat, at the cost of building two arrays.

Time O(n) · Space O(n)

Approach 3: Two maps in one pass

Keep sToT and tToS. For each position with characters a = s[i] and b = t[i]:

  1. If a is in sToT and maps to something other than b, return false.
  2. If b is in tToS and maps to something other than a, return false.
  3. Record sToT[a] = b and tToS[b] = a.

If the loop finishes, the mapping is one-to-one.

Time O(n) · Space O(k) for k distinct characters (at most 128 for ASCII)

ApproachTimeSpace
Compare every pair of positionsO(n²)O(1)
First-occurrence signaturesO(n)O(n)
Two maps in one passO(n)O(k) for k distinct characters (at most 128 for ASCII)

Solution code

Try the practice editor first. Both solutions pass every test above.

▸Isomorphic Strings solution in Python
def is_isomorphic(s: str, t: str) -> bool:
    s_to_t: dict[str, str] = {}
    t_to_s: dict[str, str] = {}
    for a, b in zip(s, t):
        if s_to_t.get(a, b) != b or t_to_s.get(b, a) != a:
            return False
        s_to_t[a] = b
        t_to_s[b] = a
    return True
▸Isomorphic Strings solution in JavaScript
function isIsomorphic(s, t) {
  const sToT = new Map();
  const tToS = new Map();
  for (let i = 0; i < s.length; i++) {
    const a = s[i];
    const b = t[i];
    if ((sToT.has(a) && sToT.get(a) !== b) || (tToS.has(b) && tToS.get(b) !== a)) {
      return false;
    }
    sToT.set(a, b);
    tToS.set(b, a);
  }
  return true;
}

Common mistakes

  • Checking only the s → t direction, which accepts "ab" and "aa".
  • Checking that the number of distinct characters matches, which is necessary but not sufficient ("abab" vs "cddc").
  • Assuming only lowercase letters. Digits, spaces, and punctuation are valid characters and need to be mapped too.
  • Comparing sorted character counts, which ignores where the characters appear.

What interviewers look for

Interviewers mostly want to see whether you catch the reverse-direction case. Say "the mapping has to be a bijection, so I'll check both directions" before you write code, and test "ab" against "aa" to prove it.

If you know the first-occurrence signature trick, mention it as an alternative; it generalizes nicely to grouping many strings by pattern.

Follow-up questions

How would you group a list of words by their isomorphism pattern?

Compute each word's first-occurrence signature (for example "noon" becomes "0,1,1,0") and use it as a hash map key. Words with the same key are all isomorphic to each other.

How does this relate to Word Pattern?

Word Pattern is the same bijection check where one side is characters of a pattern and the other side is words in a sentence. Split the sentence, check that the counts match, and run the same two-map loop.

Can you do it with fixed-size arrays instead of hash maps?

Yes, for ASCII. Keep two arrays of size 128 storing the last index (plus one) where each character was seen. At each position the two stored values must be equal; then update both to i + 1. Equal "last seen" positions everywhere means the patterns match.

Companies that ask Isomorphic Strings

Isomorphic Strings is tagged by 82 companies in our interview dataset. The score shows how often it comes up at each company.

  • Ricoh Usa100%
  • Remitly100%
  • Square Enix96%
  • West Monroe94%
  • Iterable93%
  • Dialpad86%
  • 6sense85%
  • Alcon84%
  • Coupa Software83%
  • Opendoor83%
  • Freshbooks82%
  • Nebius81%
  • Elevance Health80%
  • Intellia Therapeutics80%
  • Valon79%
  • Swiss Re79%
  • Synchrony79%
  • Biomérieux78%
  • Adevinta78%
  • Icertis77%
  • Covermymeds74%
  • Vinted73%
  • Blue Origin73%
  • Cedar72%

and 58 more.

Practice next

  • Longest Substring Without Repeating CharactersMediumSliding window with last-seen index
  • Group AnagramsMediumHash map keyed by a canonical form
  • Roman to IntegerEasyCompare each symbol with the next
  • Word LadderHardBreadth-first search on an implicit graph

Get live help in the real interview

Working solutions in real time during your actual interview. Invisible to screensharing.

Start Free Trial

No credit card required

Interview Solver
  • Home
  • About
  • Pricing
  • Sign in
  • Contact
  • Blog
  • Companion
  • Mobile
  • Help Center
  • Developers
  • Use Cases
  • AI Interview Copilot
  • Coding Interview Copilot
  • AI Interview Assistant
  • Software Engineer
  • Product Manager
  • Misc
  • Privacy Policy
  • Terms and Conditions
  • Refund & Cancellation
  • Discord Community
  • Affiliate Program
  • Compare
  • All Comparisons
  • vs Final Round AI
  • vs UltraCode
  • vs Interview Coder
  • vs LockedIn AI
  • vs AI Apply
  • Free Tools
  • ATS Resume Checker
  • STAR Method Builder
  • Salary Lookup
  • Big O Cheat Sheet
  • Total Comp Calculator
  • View All →
  • Interview Questions
  • Google Questions
  • Amazon Questions
  • Microsoft Questions
  • Meta Questions
  • Apple Questions
  • TikTok Questions
  • View All →
© 2026 Interview Solver, Inc. All rights reserved.