Why it works
- The replacement is a rule letter → letter. A letter of s must always turn into the same letter of t: toT remembers what each one turned into.
- Two different letters of s must not turn into the same letter of t: toS remembers where each letter of t came from. With toT alone, "stone" → "ladle" would pass, since n is new there.
- One clash in either map means false. If the strings end with no clash, the rule works both ways: true.
Complexity
Time O(n): one pass, map lookups are O(1). Memory O(1): each map has at most one entry per letter of the alphabet, however long the strings.
nextNow solve it yourself, no hints.
Solve on LeetCode ↗