Valid Anagram
Return true if t is an anagram of s — the same letters with the same counts, in any order.
Intuition
Anagrams have identical letter tallies. Add one for each letter of s and subtract one for each letter of t; if everything cancels to zero, they match.
Approach
Sorting works but does more than needed
Two strings are anagrams iff they have the same letters with the same counts. Sorting both and comparing proves this in O(n log n) — fine, but order is irrelevant to the question, so paying to order the characters is wasted effort. What we actually care about is the tally of each letter.
Cancel s against t in one tally
Use a 26-slot count array (lowercase letters). Walk both strings together: add 1 for each letter of s and subtract 1 for each letter of t. If the two are anagrams, every letter contributed and removed the same number of times, so all 26 counts end at exactly 0. Any imbalance leaves a non-zero entry. Counting is inherently order-independent, which is precisely why it fits this problem better than sorting.
Reject length mismatch, then verify all zeros
First a cheap guard: different lengths can't be anagrams, so return false immediately. Otherwise run the single combined loop and check that every count is zero. The count array is a fixed 26 entries regardless of input size, giving O(n) time and O(1) space.
Solution & live demo
Edge cases
An early length check returns false before any counting.
Counting is order-independent, so any rearrangement still cancels to zero.
Counts accumulate per letter, so repeats are tracked exactly and still net to zero.