Valid Anagram

Reported in interviews at BlackRock Cerence Microsoft

Problem statement

Given two strings $s$ and $t$, return true if $t$ is an anagram of $s$, and false otherwise. An anagram uses exactly the same characters with the same multiplicities, just possibly reordered.

Constraints:

  • $1 \le |s|, |t| \le 5 \times 10^4$
  • $s$ and $t$ consist of lowercase English letters (discuss the Unicode follow-up).

Examples:

  • Input: s = "anagram", t = "nagaram" -> Output: true
  • Input: s = "rat", t = "car" -> Output: false
  • Input: s = "a", t = "ab" -> Output: false (different lengths)
Show hint

What summary of a string stays the same no matter how its characters are reordered?

Relevant roles: ML Engineer, Forward Deployed Engineer and AI Engineer.

Related free problems