Back to all solutions

#1941 - Check if All Characters Have Equal Number of Occurrences

Problem 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).

Solution

/**
 * @param {string} s
 * @return {boolean}
 */
var areOccurrencesEqual = function(s) {
  const map = new Map();
  for (const char of s) {
    map.set(char, (map.get(char) || 0) + 1);
  }

  const frequencies = new Set(map.values());
  return frequencies.size === 1;
};