↓Skip to main content
  1. LeetCode/

LeetCode 205: Isomorphic Strings

·2 mins· ·
LeetCode Easy Hash-Table String
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-08-21

  • Submissions: Wrong Answer ×2 → Accepted ×2 → Wrong Answer → Accepted ×2

Approach
#

Isomorphism is a bijection: each character of s maps to one character of t and vice versa. len(set(s)) == len(set(t)) == len(set(zip(s, t))): more pairs than distinct characters means some character maps to two (Hash Table Template 5). Alternatively, replace each character with its first-occurrence index and compare the encodings.

My Attempts
#

The first versions replaced each character with its first-occurrence index:

  • Attempt 1 encoded s twice and never used t, always returning True, even for "aa", "ca".
  • Attempt 2 encoded t but concatenated the numbers without a separator, so two-digit indices collide: the last two characters of "abcdefghijkbl" and "abcdefghijklc" encode as 1, 12 and 11, 2, both "112", returning True instead of False.
  • Adding a space separator passed. A later one-line set version first forgot the return.

Solution
#

class Solution:
    def isIsomorphic(self, s: str, t: str) -> bool:
        return len(set(s)) == len(set(t)) == len(set(zip(s, t)))

Complexity
#

Time O(n), space O(k).

Mistakes to Avoid
#

  • When encoding with indices, separate the numbers; otherwise 1 + 12 and 11 + 2 both read 112.
  • Encode both strings, not s twice.

Interview Explanation
#

Isomorphism means one to one in both directions, so I check that the distinct characters of s, of t, and of the (s[i], t[i]) pairs all have the same count. Time O(n).

Related

LeetCode 13: Roman to Integer
·1 min
LeetCode Easy Hash-Table Math String
LeetCode 13 note: add each symbol’s value left to right, subtracting instead when it is smaller than the symbol to its right
LeetCode 171: Excel Sheet Column Number
·1 min
LeetCode Easy Math String
LeetCode 171 note: scan left to right with res = res × 26 + letter value (A = 1)
LeetCode 202: Happy Number
·1 min
LeetCode Easy Hash-Table Math Two-Pointers Floyds-Cycle-Finding-Algorithm
LeetCode 202 note: repeatedly take the sum of squared digits, which reaches 1 or enters a cycle; detect it with a set of seen numbers or fast and slow pointers
LeetCode 217: Contains Duplicate
·1 min
LeetCode Easy Array Hash-Table Sorting
LeetCode 217 note: if converting to a set shortens the array, there is a duplicate
LeetCode 219: Contains Duplicate II
·2 mins
LeetCode Easy Array Hash-Table Sliding-Window
LeetCode 219 note: a hash map stores each value’s last index; only the most recent occurrence needs a distance check
LeetCode 141: Linked List Cycle
·2 mins
LeetCode Easy Hash-Table Linked-List Two-Pointers Floyds-Cycle-Finding-Algorithm
LeetCode 141 note: Floyd’s cycle detection, fast two steps and slow one; with a cycle they must meet, each advancing from its own position