2423. Remove Letter To Equalize Frequency

Difficulty:
Related Topics:
    Similar Questions:

    Problem

    You are given a 0-indexed string word, consisting of lowercase English letters. You need to select one index and remove the letter at that index from word so that the frequency of every letter present in word is equal.

    Return** true if it is possible to remove one letter so that the frequency of all letters in word are equal, and false otherwise**.

    Note:

      Example 1:

    Input: word = "abcc"
    Output: true
    Explanation: Select index 3 and delete it: word becomes "abc" and each character has a frequency of 1.
    

    Example 2:

    Input: word = "aazz"
    Output: false
    Explanation: We must delete a character, so either the frequency of "a" is 1 and the frequency of "z" is 2, or vice versa. It is impossible to make all present letters have equal frequency.
    

      Constraints:

    Solution (Java)

    class Solution {
        public boolean equalFrequency(String word) {
           HashMap<Character,Integer> map=new HashMap<>();
           for(char c:word.toCharArray()) {
               map.put(c,map.getOrDefault(c,0)+1);
           }
            Set<Character> set=new HashSet(map.keySet());
            //remove 1 frequecy & check if frequency becomes equal
            for(char c: set){
                map.put(c,map.get(c)-1);
                if(map.get(c)==0){
                    map.remove(c);
                }
                if(check(map)){
                    return true;
                }
                 map.put(c,map.getOrDefault(c,0)+1);
            }
            return false;
        }
        //check all frequency become equal
        boolean check( HashMap<Character,Integer> map){
            Integer f=null;
            for(char x:map.keySet()){
                if(f==null){
                    f=map.get(x);
                }else{
                    if(f!=map.get(x)) return false;
                }
            }
            return true;
    
        }
    }
    

    Explain:

    nope.

    Complexity: