25. Check if Frequencies Can Be Equal
โ GFG solution to Check if Frequencies Can Be Equal problem: determine if removing at most one character makes all character frequencies equal. ๐
The problem can be found at the following link: ๐ Question Link
๐งฉ Problem Description
Given a string s
consisting only of lowercase alphabetic characters, check whether it is possible to remove at most one character such that the frequency of each distinct character in the string becomes the same. Return true
if it is possible; otherwise, return false
.
๐ Examples
Example 1
Input: s = "xyyz"
Output: true
Explanation: Removing one 'y' will make frequency of each distinct character to be 1.
Example 2
Input: s = "xyyzz"
Output: true
Explanation: Removing one 'x' will make frequency of each distinct character to be 2.
Example 3
Input: s = "xxxxyyzz"
Output: false
Explanation: Frequency cannot be made same by removing at most one character.
๐ Constraints
$1 \le s.size() \le 10^5$
โ
My Approach
The optimal approach uses Frequency Counting followed by Frequency Analysis:
Frequency Map Analysis
Count Character Frequencies:
Use frequency array to count occurrences of each character.
Store non-zero frequencies in a hash map with their counts.
Analyze Frequency Distribution:
Case 1: All characters have same frequency โ Return
true
Case 2: Exactly 2 different frequencies exist โ Check validity
Case 3: More than 2 different frequencies โ Return
false
Validate Two-Frequency Cases:
One frequency is 1 and occurs once (remove that single character)
Two frequencies differ by 1 and higher frequency occurs once (reduce higher frequency)
Return Result:
Return
true
if any valid case is satisfied, otherwisefalse
.
๐ Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where n is the length of the string. We iterate through the string once to count frequencies, then iterate through the frequency map (at most 26 entries) to analyze the distribution.
Expected Auxiliary Space Complexity: O(1), as we use fixed-size arrays and hash maps with at most 26 entries for lowercase letters, which is constant space.
๐งโ๐ป Code (C++)
class Solution {
public:
bool sameFreq(string& s) {
vector<int> freq(26);
for (char c : s) freq[c - 'a']++;
unordered_map<int, int> m;
for (int f : freq) if (f) m[f]++;
if (m.size() == 1) return true;
if (m.size() == 2) {
auto a = m.begin(), b = next(a);
int f1 = a->first, c1 = a->second;
int f2 = b->first, c2 = b->second;
return (f1 == 1 && c1 == 1) || (f2 == 1 && c2 == 1) ||
(abs(f1 - f2) == 1 && ((f1 > f2 && c1 == 1) || (f2 > f1 && c2 == 1)));
}
return false;
}
};
๐งโ๐ป Code (Java)
class Solution {
boolean sameFreq(String s) {
int[] freq = new int[26];
for (char c : s.toCharArray()) freq[c - 'a']++;
Map<Integer, Integer> map = new HashMap<>();
for (int f : freq) if (f > 0) map.put(f, map.getOrDefault(f, 0) + 1);
if (map.size() == 1) return true;
if (map.size() == 2) {
Iterator<Map.Entry<Integer, Integer>> it = map.entrySet().iterator();
var a = it.next(); var b = it.next();
int f1 = a.getKey(), c1 = a.getValue(), f2 = b.getKey(), c2 = b.getValue();
return (f1 == 1 && c1 == 1) || (f2 == 1 && c2 == 1) ||
(Math.abs(f1 - f2) == 1 && ((f1 > f2 && c1 == 1) || (f2 > f1 && c2 == 1)));
}
return false;
}
}
๐ Code (Python)
class Solution:
def sameFreq(self, s: str) -> bool:
from collections import Counter
freq = Counter(s)
count = Counter(freq.values())
if len(count) == 1:
return True
if len(count) == 2:
(f1, c1), (f2, c2) = count.items()
return (f1 == 1 and c1 == 1) or (f2 == 1 and c2 == 1) or \
(abs(f1 - f2) == 1 and ((f1 > f2 and c1 == 1) or (f2 > f1 and c2 == 1)))
return False
๐ง Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: ๐ฌ Any Questions?. Let's make this learning journey more collaborative!
โญ If you find this helpful, please give this repository a star! โญ
๐Visitor Count
Last updated