Check if All Characters Have Equal Number of Occurrences - Problem
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).
Input & Output
Example 1 — Good String
$
Input:
s = "abacbc"
›
Output:
true
💡 Note:
Each character appears exactly 2 times: 'a' appears 2 times, 'b' appears 2 times, 'c' appears 2 times. All frequencies are equal.
Example 2 — Bad String
$
Input:
s = "aaabb"
›
Output:
false
💡 Note:
Character 'a' appears 3 times but 'b' appears 2 times. The frequencies are not equal.
Example 3 — Single Character
$
Input:
s = "c"
›
Output:
true
💡 Note:
Only one unique character 'c' appears 1 time. All characters have the same frequency (trivially true).
Constraints
- 1 ≤ s.length ≤ 1000
- s consists of lowercase English letters
Visualization
Tap to expand
Understanding the Visualization
1
Input String
Given string with various characters
2
Count Frequencies
Count how often each character appears
3
Check Equality
Verify all frequencies are the same
Key Takeaway
🎯 Key Insight: Count character frequencies using a hash map and verify all counts are equal
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code