Blind 75 · #2 · Arrays & Hashing

Valid Anagram

EasyCharacter countingTime O(n)Space O(1)

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

true

Example 2

Input

["rat","tar"]

Output

true

Example 3

Input

["duel","lead"]

Output

false

Approach

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.

PatternCharacter counting
TimeO(n)
SpaceO(1)

Watch out for

Return false straight away when the lengths differ.

More Arrays & Hashing problems