Check if All Characters Have Equal Number of Occurrences
LeetCode 1941. Check if All Characters Have Equal Number of Occurrences asks a simple question: given a string, do all characters appear the same number of times? It is a classic frequency counting problem, and the solution boils down to two lines of real logic once you know the pattern.
The problem
Given a string s, return True if every character in s appears the same number of times, and False otherwise.
Input: s = "abacbc"
Output: True # a:2, b:2, c:2
Input: s = "aaabb"
Output: False # a:3, b:2
s = "abacbc"
Result: True
s = "aaabb"
Result: False
The approach: count and compare
The plan is simple. First, count how many times each character appears using a hash map (or Python's Counter). Then check whether all the counts are the same. If every value in the frequency map is identical, return True. Otherwise, return False.
The cleanest way to check "are all values equal?" is to dump the values into a set. A set removes duplicates. If the set has exactly one element, all counts were the same.
Converting the frequency values to a set is a one-liner way to test uniformity. If len(set(counter.values())) == 1, every character has the same count. No loops, no comparisons, just one set and one length check.
Step-by-step walkthrough
Step 1: Build the frequency map
"abacbc"{"a": 2, "b": 2, "c": 2}Scan each character in "abacbc" and count occurrences.
Step 2: Collect the frequency values
[2, 2, 2]{2}Pull out just the counts from the map.
Step 3: Check if only one unique count exists
1If the set has exactly one element, all characters appear the same number of times.
Three steps and you have the answer. The frequency map does the heavy lifting, and the set on its values gives you the final verdict in constant time.
The code
from collections import Counter
def are_occurrences_equal(s: str) -> bool:
counts = Counter(s)
return len(set(counts.values())) == 1
Counter(s)builds a dictionary mapping each character to its count. For"abacbc", this produces{"a": 2, "b": 2, "c": 2}.counts.values()extracts just the counts:[2, 2, 2].set(...)removes duplicates. If all counts are equal, the set has exactly one element.- We check
len(...) == 1. If true, every character appears the same number of times.
Alternative: manual counting
If you want to avoid importing Counter, you can build the frequency map yourself with a plain dictionary.
def are_occurrences_equal(s: str) -> bool:
freq = {}
for ch in s:
freq[ch] = freq.get(ch, 0) + 1
return len(set(freq.values())) == 1
The logic is identical. You iterate through each character, increment its count in the dictionary, and then check whether all values are the same using a set.
Complexity analysis
| Approach | Time | Space |
|---|---|---|
| Counter + set | O(n) | O(k) where k = unique chars |
You scan the string once to build the frequency map, which is O(n). Building a set from the values is O(k), where k is the number of unique characters. Since k is at most 26 for lowercase English letters, the space usage is effectively constant.
Building blocks
1. Frequency counting with a hash map
Count how often each element appears. This pattern shows up in dozens of problems, from anagram detection to majority element to top-k frequent elements.
freq = {}
for item in collection:
freq[item] = freq.get(item, 0) + 1
Or use Counter for the same result in one line:
from collections import Counter
freq = Counter(collection)
2. Checking uniformity across values
Once you have a map of values, checking whether they are all equal is a one-liner with a set. This same trick works any time you need to verify that all entries share a property.
all_equal = len(set(mapping.values())) == 1
Edge cases
- Single character string:
s = "z". Only one character, one count of 1. The set of values is{1}, so returnTrue. - All same character:
s = "aaa". One unique character with count 3. ReturnTrue. - Two characters with different counts:
s = "aab". Counts are{a: 2, b: 1}, set is{2, 1}, returnFalse. - Long string with uniform distribution: The algorithm handles this in O(n) regardless of string length.
From understanding to recall
This problem is simple enough to understand in a few minutes. But the building blocks it uses, frequency counting and value uniformity checking, appear in much harder problems. If you can write Counter(s) and set(counts.values()) without thinking, you will be faster on problems like Group Anagrams, Top K Frequent Elements, and Sort Characters By Frequency.
Spaced repetition helps you internalize these micro-patterns. CodeBricks drills the frequency counting pattern and the set-based uniformity check as separate building blocks. After a few review sessions, you will reach for them automatically whenever a problem involves counting or comparing distributions.
Related posts
- Group Anagrams - uses frequency counting as its core operation
- Contains Duplicate - another hash-based equality check
- Hash Map Patterns - overview of frequency counting and other hash map techniques
Frequency counting is one of the most common building blocks in algorithm interviews. This problem gives you the simplest possible version of it. Master it here, and you will recognize it instantly when it shows up inside harder problems. CodeBricks helps you build that recognition through spaced repetition, so the pattern is always fresh when you need it.