Why it works
- It is the check from Isomorphic Strings, only a letter stands for a whole word: toWord keeps letter → word, toLetter keeps word → letter.
- A different number of letters and words can never match, so that is checked first, before any map.
- One clash in either map means false. With no clash to the end, every letter always meets the same word and back: true.
Complexity
Time O(n), n is the length of the string: splitting it and one pass over the words. Memory O(n): the words and the two maps.
nextNow solve it yourself, no hints.
Solve on LeetCode ↗