Problem 51
Isomorphic Strings
Easy · 10 pts · String, Hash Table
Problem
Two strings S and T of the same length are isomorphic if you can turn S into T by consistently replacing letters. Every occurrence of a letter must be replaced by the same letter, and no two different letters of S may be replaced by the same letter of T. A letter may also map to itself. Decide whether S and T are isomorphic. Walk through both strings together and keep two maps, one from S to T and one from T to S; if either map ever contradicts itself, the answer is NO.
Input
Two lines: string S and string T, both lowercase and of equal length.
Output
Print YES if they are isomorphic, otherwise NO.
Constraints
1 <= |S| = |T| <= 100000
Example input
egg add
Example output
YES
Explanation
Mapping e → a and g → d turns "egg" into "add". Both g's map to the same d, and e and g map to different letters, so the strings are isomorphic: YES.