7. Valid Anagram
easyAsked at AdyenGiven two strings, return true if one is an anagram of the 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. An anagram uses exactly the same letters with the same counts.
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 compare.
- Time
- O(n log n)
- Space
- O(n)
return s.length === t.length && [...s].sort().join('') === [...t].sort().join('');Tradeoff:
2. Frequency counter
Count chars in s, decrement for t, ensure every bucket ends 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);
for (let i = 0; i < s.length; i++) {
counts[s.charCodeAt(i) - 97]++;
counts[t.charCodeAt(i) - 97]--;
}
return counts.every(c => c === 0);
}Tradeoff:
Adyen-specific tips
Adyen uses this to probe whether you reach for a fixed-size bucket array — a pattern they reuse for currency-code histograms in their fraud-signal pipeline.
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 Adyen interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →