Welcome to Subscribe On Youtube

1941. Check if All Characters Have Equal Number of Occurrences

Description

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.

Solutions

  • class Solution {
        public boolean areOccurrencesEqual(String s) {
            int[] cnt = new int[26];
            for (int i = 0; i < s.length(); ++i) {
                ++cnt[s.charAt(i) - 'a'];
            }
            int x = 0;
            for (int v : cnt) {
                if (v > 0) {
                    if (x == 0) {
                        x = v;
                    } else if (x != v) {
                        return false;
                    }
                }
            }
            return true;
        }
    }
    
  • class Solution {
    public:
        bool areOccurrencesEqual(string s) {
            int cnt[26]{};
            for (char& c : s) {
                ++cnt[c - 'a'];
            }
            int x = 0;
            for (int& v : cnt) {
                if (v) {
                    if (!x) {
                        x = v;
                    } else if (x != v) {
                        return false;
                    }
                }
            }
            return true;
        }
    };
    
  • class Solution:
        def areOccurrencesEqual(self, s: str) -> bool:
            cnt = Counter(s)
            return len(set(cnt.values())) == 1
    
    
  • func areOccurrencesEqual(s string) bool {
    	cnt := [26]int{}
    	for _, c := range s {
    		cnt[c-'a']++
    	}
    	x := 0
    	for _, v := range cnt {
    		if v > 0 {
    			if x == 0 {
    				x = v
    			} else if x != v {
    				return false
    			}
    		}
    	}
    	return true
    }
    
  • function areOccurrencesEqual(s: string): boolean {
        const cnt: number[] = new Array(26).fill(0);
        for (const c of s) {
            ++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)];
        }
        let x = 0;
        for (const v of cnt) {
            if (v) {
                if (!x) {
                    x = v;
                } else if (x !== v) {
                    return false;
                }
            }
        }
        return true;
    }
    
    
  • class Solution {
        /**
         * @param String $s
         * @return Boolean
         */
        function areOccurrencesEqual($s) {
            for ($i = 0; $i < strlen($s); $i++) {
                $hashtable[$s[$i]] += 1;
            }
            $rs = array_unique($hashtable);
            return count($rs) === 1;
        }
    }
    
    

All Problems

All Solutions