Watch 10 video solutions for Check if All Characters Have Equal Number of Occurrences, a easy level problem involving Hash Table, String, Counting. This walkthrough by Engineering Digest has 1,970 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 is not the same number of times.
Constraints:
1 <= s.length <= 1000s consists of lowercase English letters.Problem Overview: You get a string s. Every character should appear the same number of times. If all characters share an identical frequency, return true; otherwise return false. The task reduces to counting character frequencies and verifying they are equal.
Approach 1: Use a Frequency Dictionary (O(n) time, O(k) space)
The most direct solution counts how many times each character appears using a dictionary or hash map. Iterate through the string once and update the count for each character. After building the frequency map, take the first frequency as a reference and iterate through the remaining counts to ensure they match. Hash lookups and updates run in constant time, so the entire operation is linear in the length of the string. This approach relies on a hash table for fast counting and works efficiently for any string size.
Approach 2: Sort Frequencies (O(n + k log k) time, O(k) space)
Start the same way by counting character occurrences. Store all frequency values in a list and sort the list. If every element in the sorted list is equal to the first element, all characters occur the same number of times. Sorting adds a k log k cost where k is the number of distinct characters, while counting still takes O(n). This approach is slightly less efficient but very easy to reason about because identical frequencies appear consecutively after sorting. The solution still depends on character counting using concepts from counting and string processing.
Recommended for interviews: The frequency dictionary approach is what interviewers expect. It demonstrates that you can model the problem as a counting task and use constant-time hash lookups to achieve O(n) time. The sorting variant is acceptable but slightly less optimal. Showing both indicates you understand tradeoffs between direct frequency comparison and post-processing techniques like sorting.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Frequency Dictionary | O(n) | O(k) | General case. Fastest and most common interview solution using hash map counting. |
| Sort Frequencies | O(n + k log k) | O(k) | Useful when you want a simple comparison step after sorting frequency values. |