Problem Description
Given a string s, determine whether it is a good string. A string is considered good if every character in the string has the same number of occurrences.
Key Insights
- Use a hash table (or dictionary) to count the occurrence of each character.
- After constructing the frequency map, verify if all values (counts) are identical.
- Utilize a set to check the uniqueness of the frequency values; if the set size is 1, all characters occur equally.
Space and Time Complexity
Time Complexity: O(n) where n is the length of the string, since we traverse the string once to count the characters. Space Complexity: O(1) because the number of distinct characters is limited to 26 (lowercase English letters).
Solution
The solution involves iterating through the string, counting the frequency of each character using a hash table (or map). Once the counting is complete, we check if all frequency values are equal by converting them into a set. If the set's size is 1, it means all characters appear the same number of times. This is a direct and efficient O(n) solution in both time and space.