Valid Anagram

Determine if t is an anagram of s.

Pattern

Frequency Count

This problem follows the Frequency Count pattern, commonly found in the Arrays & Hashing category. Recognizing this pattern is key to solving it efficiently in an interview setting.

Approach

How to Solve It

Count character frequencies with a hash map. Compare both strings.

Key Insight

Using a fixed-size array of 26 keeps space O(1) — you don't need a hash map for lowercase English letters.

Step-by-step

  1. 1If lengths differ, return false immediately
  2. 2Create a frequency map (or array of size 26) for characters
  3. 3Increment count for each char in s, decrement for each char in t
  4. 4If all counts are zero, the strings are anagrams

Pseudocode

if len(s) != len(t): return false
count = [0] * 26
for i in range(len(s)):
    count[ord(s[i]) - ord('a')] += 1
    count[ord(t[i]) - ord('a')] -= 1
return all(c == 0 for c in count)
Complexity Analysis

Time Complexity

O(n)

Space Complexity

O(1)
More Arrays & Hashing Problems

Master this pattern with YeetCode

Practice Valid Anagram and similar Arrays & Hashing problems with flashcards. Build pattern recognition through active recall.

Practice this problem