Check whether every character in a string appears the same number of times

Quick Overview

Decide whether every character that appears in a lowercase string occurs the same number of times. The problem tests frequency counting with a hash map or a fixed array, and the edge case of ignoring letters that never appear in the string.

Check whether every character in a string appears the same number of times

Company: Bolt

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

A string is **good** if all the characters that appear in it have the same number of occurrences (the same frequency). Given a string `s`, return `True` if `s` is good and `False` otherwise. ### Function Signature ```python def are_occurrences_equal(s: str) -> bool: ... ``` ### Rules - Only characters that actually appear in `s` are compared. Letters that do not occur in `s` are ignored, so their count of zero never makes a string fail. - A string with a single distinct character is good. ### Constraints - `1 <= len(s) <= 1000` - `s` consists of lowercase English letters. ### Examples ```text Input: s = "abacbc" Output: True ``` The characters 'a', 'b' and 'c' each appear exactly 2 times. ```text Input: s = "aaabb" Output: False ``` 'a' appears 3 times and 'b' appears 2 times, so the frequencies differ. ```text Input: s = "zzzz" Output: True ``` Only 'z' appears, so there is a single frequency (4) and every present character has it.

Overview: Decide whether every character that appears in a lowercase string occurs the same number of times. The problem tests frequency counting with a hash map or a fixed array, and the edge case of ignoring letters that never appear in the string.

Bolt logo
Bolt
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

A string is good if all the characters that appear in it have the same number of occurrences (the same frequency). Given a string s, return True if s is good and False otherwise.

Function Signature

def are_occurrences_equal(s: str) -> bool:
    ...

Rules

  • Only characters that actually appear in s are compared. Letters that do not occur in s are ignored, so their count of zero never makes a string fail.
  • A string with a single distinct character is good.

Constraints

  • 1 <= len(s) <= 1000
  • s consists of lowercase English letters.

Examples

Input: s = "abacbc"
Output: True

The characters 'a', 'b' and 'c' each appear exactly 2 times.

Input: s = "aaabb"
Output: False

'a' appears 3 times and 'b' appears 2 times, so the frequencies differ.

Input: s = "zzzz"
Output: True

Only 'z' appears, so there is a single frequency (4) and every present character has it.