awelgorithms

Valid Anagram: the skills you need first

Updated

LeetCode · easy · hash maps. Same letters iff the two frequency maps are equal.

open on LeetCode

Skills this problem builds on

Stuck on Valid Anagram? It is usually one of these. Get each one solid and the solution stops being a trick.

  1. Build a frequency map

    A frequency map is value → count. One pass: for each x, put x → getOrDefault(x,0)+1. Unseen keys are absent (count 0), not stored as zeros.

  2. Compare two frequency maps

    Two bags are equal iff their frequency maps are equal: same keys, same counts. Lengths differing already forbids equality. Treat absent as 0. Do not require both maps to store zeros.

check which ones you have 10 minutes, free, no account.

More hash maps problems