08. Check if frequencies can be equal
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
I iterate through the given string and count the frequency of each character using an unordered_map.
I find the minimum and maximum frequencies among all characters.
I check if the difference between the maximum and minimum frequencies is more than 1. If it is, return 0 (false).
I count the number of characters having the minimum frequency.
I check if the difference between the total number of unique characters and the number of characters with the minimum frequency is at most 1. If it is, return 1 (true), indicating that frequencies can be made equal.
Time Complexity : O(N)
, where N is the length of the input string.
Auxiliary Space Complexity : At most, 26 unique characters can be used, resulting in a time complexity of O(26)
For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the repository.