Determine Whether Two Strings Are Isomorphic
Company: Remitly
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Onsite
Overview: Decide whether two strings have a consistent one-to-one character correspondence at every position. The exercise tests bidirectional mapping invariants, printable ASCII edge cases, constant-space reasoning, and scalable processing of large inputs.
Constraints
- 0 <= source.length, target.length <= 90,000
- Each input contains only printable ASCII characters U+0020 through U+007E
- Character identity is exact and case-sensitive
- Spaces and punctuation are ordinary characters and participate in the mapping
- Different lengths return false; two empty strings return true
- Neither input string is modified
- Let B be the compact UTF-8 JSON byte length of [source, target], with no whitespace outside strings, where quotes and reverse solidus characters use their shortest required JSON escapes and every bracket, comma, quote, escape and character byte is counted. Inputs satisfy B <= 96,000
- The serialized Boolean result uses four or five bytes, so input plus output is at most 96,005 bytes
- Target O(source.length + target.length) time and O(1) auxiliary space for the fixed printable-ASCII alphabet
Examples
Input: ('egg', 'add')
Expected Output: True
Input: ('foo', 'bar')
Expected Output: False
Hints
- Scan the two strings in lock step and decide, at each position, whether the pair you just read is compatible with every pair you have already committed to.
- Consistency from source to target is not enough: a rule that only records what each source character became will happily let two different source characters claim the same target character.
- The alphabet is fixed at 95 printable ASCII characters, so whatever bookkeeping you keep stays bounded no matter how long the strings get -- and it must survive across the whole scan, never being reset partway through.
Community answers
Answer by nisargshah1496