Maintained by Vikas Dulgunde, software engineer
You are handed two strings, s and t, both made of lowercase English letters. Return true when t is an anagram of s, and false otherwise.
Two strings are anagrams when one can be turned into the other purely by shuffling the order of its characters. Nothing is added, nothing is removed, and every letter keeps the same number of occurrences in both strings.
A quick consequence worth noticing: if the two strings have different lengths, they can never be anagrams, so that case can be rejected right away before doing any counting.
The comparison is about letter frequency, not order. "listen" and "silent" match because each holds one l, one i, one s, one t, one e, and one n, even though the letters appear in different positions.
Both strings contain three a's, one n, one g, one r, and one m, so t is a reshuffling of s.
They are the same length, but s has a t and no c while t has a c and no t, so the letter counts differ.
The lengths differ, so they cannot use the same letters the same number of times.
Visible test cases
If the strings are different lengths, you can answer false immediately without inspecting any characters.
Anagram status depends on how many times each letter shows up, not on where the letters sit. Think about counting letters rather than comparing positions.
Build a frequency tally for s, then walk through t and decrement. If any count goes negative or a leftover remains, the answer is false. Because the alphabet is fixed at 26 letters, a length-26 integer array works as the tally.
Anagrams become identical once their letters are placed in a canonical order, so sorting both strings and checking equality settles the question.
Since only 26 distinct letters are possible, a single pass that increments for s and decrements for t over a 26-slot tally tells you whether the multisets agree.