7. Valid Anagram
easyAsked at PostmanReturn true if two strings are anagrams 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. An anagram uses exactly the same characters with the same multiplicities.
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 both
Sort both strings and compare character by character.
- Time
- O(n log n)
- Space
- O(n)
return [...s].sort().join('') === [...t].sort().join('');Tradeoff:
2. Frequency counter
Maintain a 26-slot count; increment on s, decrement on t; check all zero at end.
- Time
- O(n)
- Space
- O(1)
function isAnagram(s, t) {
if (s.length !== t.length) return false;
const c = new Array(26).fill(0);
for (let i = 0; i < s.length; i++) {
c[s.charCodeAt(i) - 97]++;
c[t.charCodeAt(i) - 97]--;
}
return c.every(x => x === 0);
}Tradeoff:
Postman-specific tips
Postman expects you to call out the early-length check first, the same way request-body validators bail before parsing when content-length mismatches.
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 Postman interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →