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
Check Character Frequency EqualityInput: "abacbc"a b a c b cCount Frequenciesa: 2 occurrencesb: 2 occurrencesc: 2 occurrencesAll Equal?2 = 2 = 2 ✓Since all frequencies are equal (2), the string is goodtrue💡 Key Insight: A string is good when all characters appear the same number of times
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
Asked in
Amazon 15 Microsoft 12
28.4K Views
Medium Frequency
~10 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen