Coding · should-tier · asked at Amazon, Apple, Google, Meta, Microsoft, Netflix

Valid Anagram

easyhash-mapstring

Problem

Given two strings s and t, return True if t can be formed by rearranging the letters of s (using every letter exactly once), and False otherwise.

  • Each string has at most 80,000 characters; either may be empty.
  • Assume lowercase ASCII letters unless the interviewer says otherwise (see follow-ups for Unicode).

Examples

  • s = "brainy", t = "binary" → True. Same multiset of letters.
  • s = "apple", t = "paper" → False. t contains r, which s does not.
  • s = "moose", t = "moses" → False. Same letters, but o and s have different counts.

Hints

  1. Two strings of different lengths can never be anagrams; check that first.
  2. Being an anagram is about how many of each letter, not the order.
  3. Count letters in one string, subtract counts while reading the other, and verify nothing is left over.

Last reviewed 8 September 2026