Advertisement

Check if All Characters Have Equal Number of Occurrences - LeetCode 1941 Solution

Check if All Characters Have Equal Number of Occurrences - Complete Solution Guide

Check if All Characters Have Equal Number of Occurrences is LeetCode problem 1941, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given a string s , return true if s is a good string, or false otherwise . A string s is good if all the characters that appear in s have the same number of occurrences (i.e., the same frequency). Example 1: Input: s = "abacbc" Output: true Explanation: The characters that appear in s are 'a', 'b', and 'c'. All characters occur 2 times in s. Example 2: Input: s = "aaabb" Output: false Explanation: The characters that appear in s are 'a' and 'b'. 'a' occurs 3 times while 'b' occurs 2 times, which

Detailed Explanation

The problem asks whether all characters in a given string have the same frequency of occurrence. The input is a string `s` containing lowercase English letters. The output is a boolean value: `true` if all characters present in the string appear the same number of times, and `false` otherwise. The string's length is constrained between 1 and 1000 characters.

Solution Approach

The provided solutions all use a frequency counting approach. First, they iterate through the input string `s`, counting the occurrences of each character. In the Python solution, a dictionary is used; in Java, C++, and C, an array of size 26 is used to store the frequencies (one for each letter 'a' to 'z'). After counting, the solutions iterate through the counts. If a count is different from the first non-zero count found, the function immediately returns `false`. Otherwise, if all counts are equal or only one character was present, the function returns `true`.

Step-by-Step Algorithm

  1. Step 1: Initialize a data structure (dictionary or array of size 26) to store character frequencies. Initialize all counts to 0.
  2. Step 2: Iterate through the input string `s`. For each character, increment its corresponding frequency count in the data structure.
  3. Step 3: Iterate through the frequency counts. If this is the first non-zero count encountered, store it. If any subsequent non-zero count differs from the stored count, return `false`.
  4. Step 4: If all non-zero counts are equal to the first encountered count, return `true`.

Key Insights

  • Insight 1: Use a hash table (or array) to count the occurrences of each character. This allows for efficient counting in O(n) time.
  • Insight 2: Once character counts are tallied, iterate through them to check if all counts are equal. A simple variable to track the first encountered count simplifies the comparison logic.
  • Insight 3: The space complexity can be optimized to O(1) because we only need to store counts for 26 lowercase English letters. An array of size 26 is sufficient instead of a general hash map.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Hash Table, String, Counting.

Companies

Asked at: Bolt.