Skip to content
AI360Xpert
Beta
Difficulty: EasyArrays & Hashing

Valid Anagram

Problem in Plain English

Given two strings s and t, decide whether t is an anagram of s, meaning both strings contain exactly the same characters with exactly the same frequencies.

Problem Statement

Given two strings s and t, return true if t is an anagram of s, and false otherwise.

Two strings are anagrams when one can be rearranged into the other, which means both strings have the same length and every character appears the same number of times in each of them.

Constraints

  • 1 <= s.length, t.length <= 5 * 10^4
  • s and t consist of lowercase English letters only.

Examples

Example 1

Input
s = "anagram", t = "nagaram"
Output
true

Both strings contain three a characters and one each of n, g, r and m, so every character count matches and the answer is true.

Example 2

Input
s = "rat", t = "car"
Output
false

Both strings contain r and a, but s contains t while t contains c. Those unmatched letters make the answer false.

Example 3

Input
s = "a", t = "aa"
Output
false

The strings have different lengths, so rearranging one cannot produce the other. Return false before sorting or counting.

Example 4

Input
s = "aab", t = "abb"
Output
false

Both use the letters a and b and have length 3, but s has two a letters while t has only one. Matching sets and matching lengths do not establish matching frequencies.

Intuition

Anagrams use the same bag of letters: order can change, but no letter can be added or removed. In anagram and nagaram, both bags contain three a letters and one each of g, m, n, and r. Sorting gives both bags one common order. Counting instead assigns a balance to each letter: add occurrences from s and subtract occurrences from t; every final balance must be zero. Temporary negative balances are allowed because a matching occurrence may appear later.

Approaches

Sorting

Solution Details

Reveal Sorting: intuition, complexity, and code

Hints

Hint 1
If two strings are anagrams, what must be true about their lengths and about their sorted forms?
Hint 2
Instead of sorting, how could you tally each character as you read the strings so the tallies cancel out?
Hint 3
The alphabet has only 26 lowercase letters, so a small fixed array can hold every count with no hash map.

Edge Cases

  • Strings of different lengths, which can never be anagrams and should return false immediately.
  • Repeated letters such as s = "aacc" and t = "ccaa", where only exact frequencies make the answer true.
  • Single-character strings: a and a return true, while a and b return false.
  • Same distinct letters but different multiplicities: aab and abb return false.
  • Balanced early prefix with later mismatch: abx and bay balance after two indices, but must return false after reading the final letters.
  • Alphabet endpoints: az and za return true, exercising slots 0 and 25.
  • At length 50000, counters can reach 50000 or -50000; Java int and JavaScript numbers represent these balances safely.
  • Unicode follow-up: the 26-slot method assumes lowercase English letters. For a wider alphabet, count the intended character units in a map and clarify whether normalization is required; these snippets implement the original lowercase-only problem.

Common Mistakes and Interview Tips

  • Comparing sets instead of frequencies loses repeated letters: aab and abb use the same set but are not anagrams.
  • Returning false when a balance first becomes negative. Example 1 produces n: -1 at index 0, yet later cancels it.
  • Returning true when all prefix balances happen to be zero. abx and bay still differ at the last character.
  • Skipping the length check can cause out-of-bounds access or ignore trailing characters when the paired loop uses only s.length.
  • Using different offsets for the two strings maps the same letter to different slots. Both must subtract the code of a.
  • Interview tip: explain that two tables are also correct, but one difference table uses fewer counters. Its constant-space bound depends on a fixed alphabet; a general character map uses space proportional to distinct characters.

Key Takeaway

When a problem asks whether two inputs share the same composition, count frequencies with a hash map, or a fixed array when the alphabet is small, instead of sorting.