Skip to main content

7. Valid Anagram

easyAsked at Snap

Given two strings, decide whether one is an anagram of the other. Snap uses this to verify candidates avoid sort-based answers when a count-array is faster.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Snap loops.

  • Glassdoor (2026-Q1)Snap reports listing this with the lowercase-only follow-up question.
  • LeetCode Discuss (2025)Frequent companion to Contains Duplicate at Snap.

Problem

Given two strings s and t, return true if t is an anagram of s, and false otherwise. An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, using all the original letters exactly once.

Constraints

  • 1 <= s.length, t.length <= 5 * 10^4
  • s and t consist of lowercase English letters.

Examples

Example 1

Input
s = "anagram", t = "nagaram"
Output
true

Example 2

Input
s = "rat", t = "car"
Output
false

Approaches

1. Sort both strings

Sort s and t and compare.

Time
O(n log n)
Space
O(n)
function isAnagram(s, t) {
  if (s.length !== t.length) return false;
  return [...s].sort().join('') === [...t].sort().join('');
}

Tradeoff: Clear but loses linear time. Snap will ask you to do better since the alphabet is bounded.

2. Counts in a fixed-size array (optimal)

26-slot count array. Increment for chars in s, decrement for chars in t. Anagram iff all slots end at zero.

Time
O(n)
Space
O(1)
function isAnagram(s, t) {
  if (s.length !== t.length) return false;
  const counts = new Array(26).fill(0);
  const A = 'a'.charCodeAt(0);
  for (let i = 0; i < s.length; i++) {
    counts[s.charCodeAt(i) - A]++;
    counts[t.charCodeAt(i) - A]--;
  }
  return counts.every(c => c === 0);
}

Tradeoff: O(n) with O(1) space because the alphabet is bounded. Faster than sort and ideal for low-latency code paths.

Snap-specific tips

Snap loves the 'bounded alphabet means constant space' insight — verbalize this since most candidates instinctively reach for a HashMap. Bonus signal: extend to Unicode and explain how you would shift to a HashMap there, which mirrors how Snap handles emoji / multi-byte text in chat features.

Common mistakes

  • Forgetting the early length check — wastes a full pass.
  • Using a HashMap where a 26-slot array suffices — slower constants.
  • Hard-coding 'a' as 97 — works but charCodeAt('a') is clearer.

Follow-up questions

An interviewer at Snap may pivot to one of these next:

  • Group Anagrams (LC 49) — same pattern as a hash key.
  • Find all anagrams of p in s (LC 438) — sliding window of counts.
  • Generalize to Unicode.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why O(1) space if we use an array of 26?

Because 26 is a constant independent of n. Memory does not grow with input size.

Does increment/decrement work as cleanly with two maps?

Two maps require iterating and comparing keys at the end. The single-array trick collapses the comparison to a final 'all zeros' check, which is one less pass.

Practice these live with InterviewChamp.AI

Drill Valid Anagram and other Snap interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →