array and hashmap June 13, 2023

Check if all characters have equal number of occurrences

Time O(n) Space O(n) Open original problem

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.