Leetcode 242 Valid Anagram

Pierre-Marie Poitevin
2 min readApr 30, 2024

In this exercise, we are given 2 strings, and we should return true if and only if they are anagrams.

a. Letters frequencies

A common pattern for this type of exercises is to store the letter frequencies in an array. I usually use new int[256], so that I can do arr[c]++ for a character c instead of arr[c-’a’]++. We can simply reduce the 2 strings to 2 arrays with the frequencies for each letter.

Conversion to letter frequencies

--

--