Talentd
FleetCode

1941. Check if All Characters Have Equal Number of Occurrences

Easy79.4% acceptance4 companies

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.

Asked at 4 companies

Hints open one at a time

Hint 1
Build a dictionary containing the frequency of each character appearing in s
Hint 2
Check if all values in the dictionary are the same.
3:49
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def areOccurrencesEqual(self, s: str) -> bool:
3
Starter codePython 3Spaces: 4
Examples
Input
"abacbc"
Input
"aaabb"
Your code is saved on this device.Solution