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.

Hints:
Build a dictionary containing the frequency of each character appearing in s
Check if all values in the dictionary are the same.

GoLang: Check if All Characters Have Equal Number of Occurrences

We count the occurences and store them in a map. And then we iterate the map to see if all occurences are the same – we can return false as soon as the map size of occurences is more than 1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
func areOccurrencesEqual(s string) bool {
    var data = make(map[rune]int)
    for _, i := range s {
        data[i] ++
    }
    var d = make(map[int]int)
    for _, i := range data {
        d[i] ++
        if len(d) > 1 {
            return false;
        }
    }
    return true;
}
func areOccurrencesEqual(s string) bool {
    var data = make(map[rune]int)
    for _, i := range s {
        data[i] ++
    }
    var d = make(map[int]int)
    for _, i := range data {
        d[i] ++
        if len(d) > 1 {
            return false;
        }
    }
    return true;
}

Time and space complexity is O(N).

Python: Check if All Characters Have Equal Number of Occurrences

We use Counter object to count the occurences, and then return if the size of the set built from the Counter’s values() list is one. Two lines: O(N) time and space.

1
2
3
4
class Solution:
    def areOccurrencesEqual(self, s: str) -> bool:
        c = Counter(s)
        return len(set(c.values())) == 1
class Solution:
    def areOccurrencesEqual(self, s: str) -> bool:
        c = Counter(s)
        return len(set(c.values())) == 1

C++: Check if All Characters Have Equal Number of Occurrences

We use the unordered_map to store the key-value pairs where key is the unique characters and value is the corresponding frequency. Then we go through the map and check if the value is unique via the help of a unordered_set.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
    bool areOccurrencesEqual(string s) {
        unordered_map<char, int> data;
        for (auto &n: s) {
            data[n] ++;
        }
        unordered_set<int> d;
        for (auto &[a, b]: data) {
            d.insert(b);
            if (d.size() > 1) return false;
        }
        return true;
    }
};
class Solution {
public:
    bool areOccurrencesEqual(string s) {
        unordered_map<char, int> data;
        for (auto &n: s) {
            data[n] ++;
        }
        unordered_set<int> d;
        for (auto &[a, b]: data) {
            d.insert(b);
            if (d.size() > 1) return false;
        }
        return true;
    }
};

O(N) time and space where N is the number of characters in the given string.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
500 words
Last Post: Teaching Kids Programming - Square Matrix Diagonal Sum
Next Post: Teaching Kids Programming: Number of Positions in Line of People

The Permanent URL is: Check if All Characters Have Equal Number of Occurrences

Leave a Reply