Maintained by Vikas Dulgunde, software engineer
You are handed two strings, s and t. Your job is to report whether t can be formed by shuffling the letters of s without adding or dropping any.
Two strings qualify as anagrams when every distinct character appears the same number of times in both. Order does not matter at all, only the tally of each character.
A quick consequence: if the two strings differ in length, they cannot be anagrams, since one side must hold a character the other lacks. The inputs here are lowercase English letters.
Both strings contain three a, one n, one g, one r, and one m, so t is just a reordering of s.
They share the same length, but s has a t while t (the string) has a c, so the letter tallies do not match.
The lengths differ, so no rearrangement of one can ever equal the other.
Visible test cases
If the two strings have different lengths, you can answer false right away.
Anagrams are about counts, not order. How could you record how many times each letter shows up?
Tally every character in s, then walk through t and decrement. If any count goes negative or a letter is missing, t is not an anagram.
Two strings are anagrams exactly when their sorted character sequences are identical.
Count how often each character appears in s, then spend those counts down while scanning t; any shortfall means they are not anagrams.