easyHash maps and sets target 15 min
Valid anagram
Two strings are anagrams when one is a rearrangement of the other: the same letters, each appearing the same number of times in both. You get two strings, s and t. Say whether they are anagrams of each other: return True or False.
Two empty strings count as anagrams of each other.
Example 1
Input
s = "stop", t = "pots"OutputTrueBoth have one s, one t, one o and one p.
Example 2
Input
s = "pool", t = "plop"OutputFalseSame three letters, different counts: "pool" has two o's and one p, "plop" has one o and two p's.
Example 3
Input
s = "ten", t = "tent"OutputFalseStrings of different lengths can never be anagrams.
Constraints
0 ≤ len(s), len(t) ≤ 105
Both strings hold only the lowercase letters a to z.
Plan it first
Write a line for each before you code, then say them out loud. Compare with the Approach tab afterwards.
Run examples checks the examples. Submit runs every test, including edge cases and, when the problem has one, a speed check on a large input.