Isomorphic Strings
Easy · hash map
You are given two strings s and t of the same length. Think of renaming letters: pick a letter that appears in s and replace every copy of it with some other letter. Do this for as many letters as you like, all at once. A letter may also keep its own name.
The renaming must be one-to-one: two different letters of s can never end up with the same new letter.
Return true if some renaming turns s into exactly t, and false otherwise.
Examples
Example 1
Input: s = "egg", t = "add"
Output: true
Rename e to a and g to d. Both g's change together.
Example 2
Input: s = "foo", t = "bar"
Output: false
Both o's in s must change to the same letter, but they line up with a and r.
Example 3
Input: s = "ab", t = "aa"
Output: false
a and b would both have to become a, and a renaming may not merge two letters.
Constraints
- 1 <= len(s) <= 10^4
- len(t) == len(s)
- s and t contain only lowercase English letters.