Check if All Characters Have Equal Number of Occurrences – Frequency Map Approach (LeetCode 1941)
Check if All Characters Have Equal Number of Occurrences – Frequency Map Approach (LeetCode 1941)

Check if All Characters Have Equal Number of Occurrences – Frequency Map Approach (LeetCode 1941)

Simple HashMap Technique to Validate Equal Character Frequencies

17 views
0
0

🔗 Problem Link

LeetCode 1941 – Check if All Characters Have Equal Number of Occurrences

👉 https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/

Introduction

This is one of those problems that looks very simple at first glance — and it actually is — but it helps strengthen your understanding of frequency counting using HashMap.

The problem asks us to determine whether all characters in a string occur the same number of times.

No sliding window.

No binary search.

Just clean frequency logic.

But even simple problems help build strong foundations.

📌 Problem Understanding

A string is considered "good" if:

  1. Every character that appears in the string
  2. Appears the same number of times

If even one character has a different frequency → return false.

Example 1

Input: s = "abacbc"
Output: true

Character counts:

  1. a → 2
  2. b → 2
  3. c → 2

All equal → ✔ true

Example 2

Input: s = "aaabb"
Output: false

Character counts:

  1. a → 3
  2. b → 2

Not equal → ✘ false

🧠 Approach & Intuition

When I saw this problem, my thinking was:

  1. Count the frequency of every character.
  2. Compare all frequencies.
  3. If all are equal → return true.

The important part is choosing a reference frequency and comparing everything against it.

💻 Your Code

class Solution {
public boolean areOccurrencesEqual(String s) {
HashMap<Character,Integer> mp = new HashMap<>();
int ref =0;
char c = s.charAt(0);
for(int i =0 ; i< s.length();i++){
if(c == s.charAt(i)){
ref++;
}
mp.put(s.charAt(i),mp.getOrDefault(s.charAt(i),0)+1);
}

for(int a:mp.values()){
if(ref != a){
return false;
}
}
return true;
}
}

🔍 Step-by-Step Explanation

1️⃣ Initialize HashMap

HashMap<Character,Integer> mp = new HashMap<>();

This stores frequency of each character.

2️⃣ Choose Reference Character

char c = s.charAt(0);
int ref = 0;

You use the first character as a reference.

Then count how many times it appears while also building the frequency map.

3️⃣ Build Frequency Map

mp.put(s.charAt(i), mp.getOrDefault(s.charAt(i), 0) + 1);

This line increases count for each character.

4️⃣ Compare Frequencies

for(int a : mp.values()){
if(ref != a){
return false;
}
}

If any frequency differs from the reference count → return false.

Otherwise → true.

⏱ Time and Space Complexity

Time Complexity: O(n)

  1. One loop to count frequencies
  2. One loop over at most 26 characters

Space Complexity: O(26) ≈ O(1)

Only lowercase English letters are allowed.

🔥 Small Optimization Idea

Your solution works perfectly.

However, we can simplify it slightly:

Instead of separately counting the reference frequency, we can:

  1. First build the entire frequency map.
  2. Take the frequency of the first character from the map.
  3. Compare all values with it.

Cleaner Version

class Solution {
public boolean areOccurrencesEqual(String s) {
HashMap<Character,Integer> mp = new HashMap<>();
for(char ch : s.toCharArray()){
mp.put(ch, mp.getOrDefault(ch, 0) + 1);
}

int ref = mp.get(s.charAt(0));
for(int freq : mp.values()){
if(freq != ref){
return false;
}
}
return true;
}
}

Same logic — slightly cleaner structure.

🎯 Key Learning from This Problem

This problem reinforces:

  1. Frequency counting using HashMap
  2. Using a reference value for comparison
  3. Clean loop logic
  4. Early return for optimization

Even though it is an easy problem, it builds the base for harder problems like:

  1. Valid Anagram
  2. Group Anagrams
  3. First Unique Character
  4. Ransom Note

🏁 Final Thoughts

Problems like this are not about complexity.

They are about:

  1. Writing clean logic
  2. Handling frequency maps properly
  3. Thinking clearly about conditions

Mastering easy problems makes medium and hard problems much easier later.

Ai Assistant Kas