Skip to main content

28. Isomorphic Strings

easyAsked at Snowflake

Decide whether two strings are isomorphic — there's a one-to-one mapping between characters. Snowflake asks this to test bijection logic — the same shape used to validate schema-to-schema column mappings during data sharing.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Snowflake loops.

  • Glassdoor (2025-Q4)Snowflake data-sharing team uses this to set up schema-mapping follow-up.
  • LeetCode Discuss (2025-09)Reported at Snowflake SDE-I screens.

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^4
  • t.length == s.length
  • s and t consist of any valid ASCII character.

Examples

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

Approaches

1. Single map (broken)

Track s->t only; misses the case where two s-chars map to the same t-char.

Time
O(n)
Space
O(1)
function isIsomorphic(s, t) {
  const map = new Map();
  for (let i = 0; i < s.length; i++) {
    if (map.has(s[i])) {
      if (map.get(s[i]) !== t[i]) return false;
    } else {
      map.set(s[i], t[i]);
    }
  }
  return true;
}

Tradeoff: Fails for ('ab', 'aa') — passes the s->t map but misses the t-side collision. Bug, not solution.

2. Two maps (optimal)

Track s->t AND t->s. Both maps must agree.

Time
O(n)
Space
O(1)
function isIsomorphic(s, t) {
  const sToT = new Map();
  const tToS = new Map();
  for (let i = 0; i < s.length; i++) {
    const a = s[i], b = t[i];
    if (sToT.has(a) && sToT.get(a) !== b) return false;
    if (tToS.has(b) && tToS.get(b) !== a) return false;
    sToT.set(a, b);
    tToS.set(b, a);
  }
  return true;
}

Tradeoff: Both directions of the bijection are enforced. The bug from the one-map version is gone.

Snowflake-specific tips

Snowflake interviewers grade this on whether you spot the need for a bijection (not a function). Bonus signal: connect to data sharing — when Snowflake provider/consumer maps columns across accounts, they enforce a similar bijection so no two source columns collapse onto one target column.

Common mistakes

  • Using only one map (s->t), missing the t-side collision case.
  • Comparing positions of first occurrence instead of building a map — works but is harder to reason about.
  • Forgetting that 'a maps to a' is allowed (identity mapping).

Follow-up questions

An interviewer at Snowflake may pivot to one of these next:

  • Word Pattern (LC 290) — bijection between pattern chars and words.
  • How does Snowflake validate column mappings in data shares?
  • Identity transformation as a special case.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why do I need two maps?

A function s->t lets two source chars share a target. A bijection requires the inverse t->s also to be a function. Two maps enforce both directions.

When is one map enough?

Never for isomorphism. But for 'does s become t via a one-way substitution that may collapse', one map suffices. Read the problem carefully.

Practice these live with InterviewChamp.AI

Drill Isomorphic Strings and other Snowflake interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →