Skip to main content

28. Isomorphic Strings

easyAsked at Salesforce

Determine if two strings are isomorphic (bijective character mapping). Salesforce uses this to test bidirectional hash-map invariants — they grade on whether you check BOTH directions.

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

Source citations

Public interview reports confirming this problem appears in Salesforce loops.

  • Glassdoor (2026-Q1)Salesforce uses bidirectional mapping in their field-mapping pipelines.
  • LeetCode Discuss (2025-09)Common one-way-mapping trap question.

Problem

Given two strings s and t, determine if they are isomorphic. Two strings 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-direction map

Build a map s_char -> t_char; check that occurrences map consistently.

Time
O(n)
Space
O(k)
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 on inputs like 'ab' -> 'aa' (a maps to a, b maps to a — both directions broken, but only forward is checked). Salesforce specifically tests this gotcha.

2. Bidirectional map

Maintain s->t and t->s maps. Any mismatch in either direction returns false.

Time
O(n)
Space
O(k)
function isIsomorphic(s, t) {
  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) && 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: Two maps but still O(n) time and O(k) space. The two-way check enforces bijectivity.

Salesforce-specific tips

Salesforce explicitly tests with cases like 'ab' -> 'aa' to catch one-way-mapping bugs. They grade on whether you understand that 'isomorphic' is symmetric. Bonus signal: mention this same pattern in Word Pattern (LC 290) and Word Pattern II (LC 291) — the second uses backtracking.

Common mistakes

  • Building only one direction — 'ab' -> 'aa' passes but is wrong.
  • Confusing isomorphic with anagram — isomorphic preserves order; anagram doesn't.
  • Using an object with string keys when characters could collide with __proto__ — use a Map.

Follow-up questions

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

  • Word Pattern (LC 290) — same pattern, words instead of chars.
  • Encode and Decode Strings (LC 271).
  • Why does single-direction fail? Construct a counterexample on the spot.

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 doesn't single-direction work?

Because s -> t consistency doesn't imply t -> s consistency. If both 'a' and 'b' in s map to 'a' in t, the forward map agrees ('b' has no prior mapping) but the function isn't bijective.

Is there an O(1)-space version?

For ASCII (256 chars), two fixed arrays of size 256 suffice — O(256) = O(1). Same idea as the map but with array lookup.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →