Check if all characters have equal number of occurrences

June 13, 2023

array-and-hashmap

Problem URL: Check if all characters have equal number of occurrences

We will count all the occurance of the characters in the string and store it in a hashmap. Then we will check if all the values in the hashmap are equal or not.

class Solution:
    def areOccurrencesEqual(self, s: str) -> bool:
        count = collections.Counter(s).values()
        return len(set(count)) == 1

Time Complexity: O(n) where n is the length of the string.
Space Complexity: O(n) where n is the length of the string.