Skip to main content

29. Isomorphic Strings

easyAsked at Workday

Determine if two strings are isomorphic — characters in s can be replaced to get t with a consistent one-to-one mapping. Workday uses this for column-mapping integrity checks during HRIS imports.

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

Source citations

Public interview reports confirming this problem appears in Workday loops.

  • Glassdoor (2025)Workday HRIS-import team — mapping validation.

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)

Map each s[i] -> t[i]; reject on conflict.

Time
O(n)
Space
O(1) for ASCII)
// fails on ('ab', 'aa') — 'a' and 'b' both map to 'a', but no map detects this.

Tradeoff: Misses 'two source chars mapping to same target'. Don't ship.

2. Two maps (one each direction)

Maintain s->t and t->s maps. Both must be consistent.

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

Tradeoff: Two maps catch both 'inconsistent forward' and 'two source chars to same target'.

Workday-specific tips

Workday grades the 'one-to-one' part. A single map only catches forward conflicts; the second map is the entire trick. State this before coding so they see you've understood the constraint.

Common mistakes

  • Using only one map — misses ('ab', 'aa') style cases.
  • Using JSON or strings as composite keys — slow.
  • Forgetting the length check at the start.

Follow-up questions

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

  • Word Pattern (LC 290) — same pattern, words instead of characters.
  • Group Anagrams (LC 49).
  • What if non-ASCII?

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 two maps?

A one-to-one mapping requires both directions. Forward map (s -> t) catches 'a maps to different things'. Reverse (t -> s) catches 'different sources both map to the same target'.

Can I do this with a single string transformation?

Yes — encode each string by 'first-occurrence index' (s='egg' -> '0,1,1', t='add' -> '0,1,1'). Compare encoded strings. Same complexity.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →