242. Valid Anagram
easyAsked at GoDaddyCheck if two strings contain the exact same characters in the same counts — GoDaddy uses this hash-map frequency pattern in their domain-suggestion pipeline to detect whether two brand-name candidates are character-for-character rearrangements of each other.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
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 and check equality — simple but O(n log n).
- Time
- O(n log n)
- Space
- O(n)
function isAnagram(s, t) {
if (s.length !== t.length) return false;
return s.split('').sort().join('') === t.split('').sort().join('');
}Tradeoff:
2. Character frequency count
Increment a 26-element array for s, decrement for t; any non-zero entry means the strings differ.
- Time
- O(n)
- Space
- O(1)
function isAnagram(s, t) {
if (s.length !== t.length) return false;
const count = new Array(26).fill(0);
for (let i = 0; i < s.length; i++) {
count[s.charCodeAt(i) - 97]++;
count[t.charCodeAt(i) - 97]--;
}
return count.every(c => c === 0);
}Tradeoff:
GoDaddy-specific tips
GoDaddy uses Valid Anagram as a warm-up to confirm you know O(n) frequency counting, and then immediately extends it to Unicode strings — ask about the follow-up: with Unicode characters you'd use a Map instead of the fixed 26-element array, which shows you understand the constraint boundary.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill Valid Anagram and other GoDaddy interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →