Check if All Characters Have Equal Number of Occurrences

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

May 12, 2025 - 15:56
 0
Check if All Characters Have Equal Number of Occurrences

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 <= 1000
s consists of lowercase English letters.

class Solution:
    def areOccurrencesEqual(self, s: str) -> bool:
        from collections import Counter

        freq = Counter(s)
        values = set(freq.values())
        return len(values) == 1

✅ Example 1: s = "abacbc"

freq = Counter(s) # {'a': 2, 'b': 2, 'c': 2}
freq.values() # dict_values([2, 2, 2])
set(freq.values()) # {2}
len(set(freq.values())) # 1 → True ✅
✔️ All characters occur 2 times → set has just one unique value (2)

❌ Example 2: s = "aaabb"

freq = Counter(s) # {'a': 3, 'b': 2}
freq.values() # dict_values([3, 2])
set(freq.values()) # {2, 3}
len(set(freq.values())) # 2 → False ❌
✖️ Frequencies differ (3 and 2) → set has two values → return False

✅ Example 3: s = "xyz"

freq = Counter(s) # {'x': 1, 'y': 1, 'z': 1}
freq.values() # dict_values([1, 1, 1])
set(freq.values()) # {1}
len(set(freq.values())) # 1 → True ✅
✔️ Each character occurs once → return True

❌ Example 4: s = "aaabbbcc"

freq = Counter(s) # {'a': 3, 'b': 3, 'c': 2}
freq.values() # dict_values([3, 3, 2])
set(freq.values()) # {2, 3}
len(set(freq.values())) # 2 → False ❌
✖️ Different counts → return False

✅ Example 5: s = "a"

freq = Counter(s) # {'a': 1}
freq.values() # dict_values([1])
set(freq.values()) # {1}
len(set(freq.values())) # 1 → True ✅
✔️ Only one character → always valid → return True

✅ Time Complexity:
Counter(s):

This goes through the entire string s once to count frequencies.

Time: O(n) where n is the length of the string.

freq.values():

Extracts the frequency values, which is proportional to the number of unique characters.

Worst case (e.g., all characters different): O(n)

set(freq.values()):

Building a set from up to n frequency values.

Time: O(k) where k is the number of unique characters (bounded by 26 if only lowercase letters).

len(values) == 1:

Constant time: O(1)