Valid Anagram
Valid Anagram is an easy Arrays & Hashing problem from the Blind 75. The key pattern is character counting, and a good solution runs in O(n) time.
Problem
Given two lowercase strings, determine whether they contain exactly the same character counts.
Examples
Example 1
Input
["silent","listen"]Output
trueExample 2
Input
["rat","tar"]Output
trueExample 3
Input
["duel","lead"]Output
falseApproach
Count letters of the first string up and the second string down in a 26-slot array. The strings are anagrams exactly when every slot ends at zero.
| Pattern | Character counting |
|---|---|
| Time | O(n) |
| Space | O(1) |
Watch out for
Return false straight away when the lengths differ.