Valid Anagram

Return true if t is an anagram of s — the same letters with the same counts, in any order.

A way to picture it

Two Scrabble players claim their racks hold the same tiles. Keep one tally board: every tile from player one adds a mark, every tile from player two rubs one out. Same tiles means a spotless board at the end.

How to approach it

Count letters up for s and down for t. They're anagrams exactly when every count returns to zero.

Example

Input: {"s":"anagram","t":"nagaram"}. Output: true.

Complexity: O(n) time and O(26) space. One pass; the tally has at most 26 letters.

This page includes an animated walkthrough, Java and Python solutions, and a question to check your understanding.