1941. Check if All Characters Have Equal Number of Occurrences LeetCode Solution

In this guide, you will get 1941. Check if All Characters Have Equal Number of Occurrences LeetCode Solution with the best time and space complexity. The solution to Check if All Characters Have Equal Number of Occurrences problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Check if All Characters Have Equal Number of Occurrences solution in C++
  4. Check if All Characters Have Equal Number of Occurrences solution in Java
  5. Check if All Characters Have Equal Number of Occurrences solution in Python
  6. Additional Resources
1941. Check if All Characters Have Equal Number of Occurrences LeetCode Solution image

Problem Statement of 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.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(26) = O(1)

1941. Check if All Characters Have Equal Number of Occurrences LeetCode Solution in C++

class Solution {
 public:
  bool areOccurrencesEqual(string s) {
    vector<int> count(26);
    for (const char c : s)
      ++count[c - 'a'];
    return equalFreq(count, count[s[0] - 'a']);
  }

 private:
  bool equalFreq(const vector<int>& count, int theFreq) {
    return ranges::all_of(
        count, [theFreq](int freq) { return freq == 0 || freq == theFreq; });
  }
};
/* code provided by PROGIEZ */

1941. Check if All Characters Have Equal Number of Occurrences LeetCode Solution in Java

class Solution {
  public boolean areOccurrencesEqual(String s) {
    int[] count = new int[26];
    for (final char c : s.toCharArray())
      ++count[c - 'a'];
    return equalFreq(count, count[s.charAt(0) - 'a']);
  }

  private boolean equalFreq(int[] count, int theFreq) {
    return Arrays.stream(count).allMatch(freq -> freq == 0 || freq == theFreq);
  }
}
// code provided by PROGIEZ

1941. Check if All Characters Have Equal Number of Occurrences LeetCode Solution in Python

class Solution:
  def areOccurrencesEqual(self, s: str) -> bool:
    return len(set(collections.Counter(s).values())) == 1
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.