Understanding Valid Anagram
Return whether s and t contain exactly the same letters with the same frequencies.
How it works
Count letters in one string and cancel them with letters from the other.
Reject unequal lengths. Count each letter in s. Consume counts using t and reject any missing letter.
Try an example
Input: s = "anagram", t = "nagaram"
Output: true
Count letters in one string and cancel them with letters from the other.
Constraints
- Each string contains at most 64 lowercase English letters.
- Two empty strings are anagrams.
Use the simulator
Change the input, then choose Visualize input. Use Next and Previous to inspect each decision, or Play to advance automatically. Compare approaches and their complexity. Choose Try yourself to write JavaScript, select My code and Visualize code to inspect its execution, or Run Tests to check results.
Execution happens in your browser. Drafts are saved locally; avoid entering secrets or sensitive information.