LeetCode-in-Java

1941. Check if All Characters Have Equal Number of Occurrences

Easy

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:

Solution

public class Solution {
    public boolean areOccurrencesEqual(String s) {
        int[] counter = new int[26];
        for (int i = 0; i < s.length(); i++) {
            counter[s.charAt(i) - 'a']++;
        }
        int bench = 0;
        for (int i = 0; i < 26; i++) {
            if (bench == 0) {
                if (counter[i] != 0) {
                    bench = counter[i];
                }
            } else {
                if (counter[i] != 0 && counter[i] != bench) {
                    return false;
                }
            }
        }
        return true;
    }
}