Easy · Frequency Counting
Understand → Visualise → Practice.
Return whether two strings contain exactly the same characters with the same frequencies.
Two strings are anagrams exactly when every character occurs the same number of times in both.
Different lengths can't be anagrams.
Add 1 for each character of a and subtract 1 for each character of b.
They are anagrams if every count ends at 0.
O(n) time and O(k) space for k distinct characters.
A set loses duplicate counts, so it cannot distinguish "abb" from "ab".
Read the full guide first: Strings: Frequencies, Matching and Palindromes.
Guest mode lets you understand, visualise and run the sample tests. Create a free account to submit and save your solutions.