Why it works
- Anagrams have the same letters the same number of times, only in a different order. So compare counts, not positions.
- s adds one for each of its letters, t takes one away. A count below 0 means t has that letter more times than s: false at once.
- The lengths are equal, so if nothing went below 0, nothing is left above 0 either: every count is 0, and the answer is true.
Complexity
Time O(n): one pass over each word. Memory O(1): at most 26 counts for lowercase letters, however long the words.
nextNow solve it yourself, no hints.
Solve on LeetCode ↗