Skip to main content

11. Valid Anagram

easyAsked at Coupang

Decide whether two strings are anagrams, mirroring how Coupang's Korean e-commerce search ranking normalizes query tokens.

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^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 and compare

Sort both strings; equal sorted strings means anagram.

Time
O(n log n)
Space
O(n)
return s.split('').sort().join('') === t.split('').sort().join('');

Tradeoff:

2. Frequency count

Tally chars in s, decrement using t, and confirm all counts hit zero.

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:

Coupang-specific tips

Coupang's Korean e-commerce search ranking normalizes Hangul Jamo tokens before lookup; frequency-count anagram logic generalizes to that token-normalization pattern.

Solve it now

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

Output

Press Run or Cmd+Enter to execute

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →