File: check-if-all-characters-have-equal-number-of-occurrences/solution.py

Date: 2026-06-06

Time: 15:36

Purpose

This file solves LeetCode 1941: Check if All Characters Have Equal Number of Occurrences. Given a string s, it returns True if every distinct character appears the same number of times. It's a classic frequency-uniformity check.

Key Components

Solution.areOccurrencesEqual(self, s: str) -> bool — The core method. Counts character frequencies with Counter, extracts the frequency values, converts them to a set, and checks that the set has exactly one element. If all characters share the same count, the set of counts is {k} for some k, so its length is 1.

makestringsorted — A module-level alias that binds Solution().areOccurrencesEqual as a bare function. This is the repo's convention for exposing solutions to the test harness. The name makestringsorted is a misnomer — it doesn't match the problem — likely a copy-paste artifact from the repo's code generation tooling.

Patterns

Dependencies

Imports: collections.Counter — standard library, no external deps.

Imported by: The massive importedby list is misleading. Those are test files for *other* problems, not consumers of this solution's logic. The actual consumer is check-if-all-characters-have-equal-number-of-occurrences/testsolution.py. The cross-references likely reflect a shared test runner or import pattern rather than real code dependencies.

Flow

1. Counter(s) builds a {char: count} mapping in O(n).

2. .values() yields the counts (a dict_values view).

3. set(...) deduplicates the counts.

4. len(...) == 1 checks that exactly one distinct count exists.

For s = "abacbc": Counter gives {'a':2, 'b':2, 'c':2}, values are [2,2,2], set is {2}, length is 1 → True.

For s = "aaabb": Counter gives {'a':3, 'b':2}, values are [3,2], set is {3,2}, length is 2 → False.

Invariants

Error Handling

None. The function assumes valid input per LeetCode guarantees. An empty string would produce an empty Counter, an empty set, and len(set()) == 1False, which is a reasonable degenerate answer but not explicitly handled.

Topics to Explore

Beliefs