Isomorphic Strings

EasyArrays & Hashingbijection map

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Two strings s and t are called **isomorphic** if the characters in s can be replaced to produce t. Every occurrence of a character must be mapped to a single other character while keeping the order of characters the same. Two different characters in s may **not** map to the same character in t, but a character is always allowed to map to itself. Given two strings s and t, return true if they are isomorphic, and false otherwise. In other words, the mapping between the characters of s and t must be a **bijection**: each character of s maps to exactly one character of t, and no two distinct characters of s share the same image.

Examples

Input: s = "egg", t = "add"
Output: true
Map `e` -> `a` and `g` -> `d`. Applying these replacements to `egg` yields `add`, and the mapping is consistent and one-to-one.
Input: s = "foo", t = "bar"
Output: false
The two `o` characters in `foo` would have to map to both `a` and `r`, which is impossible for a single consistent mapping.
Input: s = "paper", t = "title"
Output: true
Map `p` -> `t`, `a` -> `i`, `e` -> `l`, `r` -> `e`. Every character maps consistently and no two source characters collide on the same target.

Constraints

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

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More bijection map problems