[LeetCode]#1941. Check if All Characters Have Equal Number of Occurrences

Environment: Python 3.8

Key technique: set, Counters

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.

Analysis:

  1. Counter it as {‘a’:2, ‘b’:2, ‘c’:2}
  2. Check value is equal or not.

Solution:

from collections import Counter
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
ans=set(Counter(s).values())
return len(ans)==1

Submissions: