AlgoFrames

Isomorphic strings

Standard · Write code · about 12 min

Two strings s and t are isomorphic when you can replace the letters of s to get t. Every copy of a letter must become the same letter, and two different letters may not become the same letter. A letter may stay as it is.

Return true if s and t are isomorphic.

Example 1

Input:

["egg","add"]

Output:

true

e → a and g → d.

Example 2

Input:

["foo","bar"]

Output:

false

o would have to become both a and r.

Example 3

Input:

["badc","baba"]

Output:

false

b and d would both become b.

Constraints

  • 0 ≤ s.length ≤ 10,000
  • t.length = s.length
  • s and t hold printable ASCII characters

Learn the idea first: Hash maps for lookup and counting, a visual lesson.

More Hash maps for lookup and counting problems