Isomorphic Strings
EasyHash Map · LinkedIn
You are given two strings s and t of the same length. They are isomorphic if you can replace characters in s to get t such that:
- every occurrence of a character is replaced by the same character (the mapping is consistent), and
- no two different characters of
sare replaced by the same character (the mapping is one-to-one).
A character may map to itself. Return True if s and t are isomorphic, otherwise False.
Example 1
- Input:
- s = "noon", t = "peep"
- Output:
- True
- Explanation
- Map
n -> pando -> e. Everynbecomespand everyobecomese, and the two letters map to different characters.
Example 2
- Input:
- s = "ab", t = "cc"
- Output:
- False
- Explanation
- Both
aandbwould have to map toc, but two different characters cannot share a target.
Example 3
- Input:
- s = "cc", t = "ab"
- Output:
- False
- Explanation
- The first
cwould map toaand the second tob, but a character must always map to the same thing.
Constraints
- 1 ≤
s.length≤ 5 * 104 t.length == s.lengthsandtconsist of printable ASCII characters