242. Valid Anagram
easyAsked at AppleValid Anagram is the Apple warm-up that grades whether you pick the right approach for the constraint. Sort-both is the lazy answer; frequency-count is the optimal. The Unicode follow-up is what Apple uses to separate easy passes from strong easy passes.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Apple loops.
- Glassdoor (2026-Q1)— Apple SWE phone-screen reports list Valid Anagram as the 10-minute string warm-up with the Unicode follow-up.
- Blind (2025-11)— Apple new-grad interview reports flag Valid Anagram as a recurring easy.
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, typically using all the original letters exactly once.
Constraints
1 <= s.length, t.length <= 5 * 10^4s and t consist of lowercase English letters.
Examples
Example 1
s = "anagram", t = "nagaram"trueExample 2
s = "rat", t = "car"falseApproaches
1. Sort and compare
Sort both strings, compare directly.
- Time
- O(n log n)
- Space
- O(n) for sort buffer
function isAnagram(s, t) {
if (s.length !== t.length) return false;
return s.split('').sort().join('') === t.split('').sort().join('');
}Tradeoff: Easy to write, accepted for correctness. Mention it then offer the linear version — Apple grades this on whether you reach for the optimal unprompted.
2. Frequency count (optimal)
If lengths differ, return false. Build a count array (26 slots) by incrementing for s and decrementing for t. All zeros at the end means anagram.
- Time
- O(n)
- Space
- O(1) — 26 fixed slots
function isAnagram(s, t) {
if (s.length !== t.length) return false;
const count = new Array(26).fill(0);
const A = 'a'.charCodeAt(0);
for (let i = 0; i < s.length; i++) {
count[s.charCodeAt(i) - A]++;
count[t.charCodeAt(i) - A]--;
}
return count.every(c => c === 0);
}Tradeoff: Linear time, constant space. The single-pass +/- trick avoids two separate count arrays. Apple's preferred answer.
3. Frequency count with Map for Unicode
Same idea but use a Map keyed by char. Increment for s, decrement for t. All zero values = anagram.
- Time
- O(n)
- Space
- O(alphabet)
function isAnagram(s, t) {
if (s.length !== t.length) return false;
const count = new Map();
for (const ch of s) count.set(ch, (count.get(ch) ?? 0) + 1);
for (const ch of t) count.set(ch, (count.get(ch) ?? 0) - 1);
for (const v of count.values()) if (v !== 0) return false;
return true;
}Tradeoff: Same complexity. Apple's Unicode follow-up: 'what if the inputs were arbitrary Unicode?' — switch from the 26-array to this Map. Mention this version even if you lead with the array.
Apple-specific tips
Apple specifically asks the Unicode follow-up. Lead with the 26-array version because the constraints say lowercase only, then proactively say: 'if the inputs were Unicode I'd switch to a Map keyed by code point — same O(n) time, the space becomes O(alphabet).' That sentence is what separates a 7/10 from a 9/10 on this question.
Common mistakes
- Skipping the length-equality check up front — saves you n work on obvious non-anagrams.
- Using two separate count maps and comparing them — works but is twice the space and one extra pass.
- Using charCodeAt without subtracting 'a'.charCodeAt(0) — off-by-26 index in the array.
Follow-up questions
An interviewer at Apple may pivot to one of these next:
- Group Anagrams (LC 49) — categorize a list of strings by anagram class.
- Find All Anagrams in a String (LC 438) — sliding window with frequency.
- What if memory was constrained — do you need both passes?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why is the frequency count O(1) space?
The alphabet is fixed at 26. The count array does not grow with n, so for asymptotic analysis it's constant.
Sort or frequency count?
Both pass. Frequency is strictly better — O(n) vs O(n log n). Lead with frequency; mention sort only if asked for an alternative.
Free learning resources
Curated free links for this problem.
Practice these live with InterviewChamp.AI
Drill Valid Anagram and other Apple interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →