Easy HashMap Java Solution
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n\n1) Initialize a HashMap freqMap to store the frequency of each characte
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n\n1) Initialize a HashMap freqMap to store the frequency of each character in the word.\n2) Iterate through each character in the input word and update the frequency count in the freqMap.\n3) For each character c in the word:\nIf c is not already in the freqMap, add it with a frequency of 1.\nIf c is already in the freqMap, increment its frequency by 1.\n4) Initialize a variable min to store the minimum number of deletions needed.\n5) Iterate through the values of the freqMap, representing the frequencies of characters in the word.\n6) For each frequency freq:\n- Initialize a variable curr to calculate the current number of deletions for the current frequency.\n- Iterate through the values of the freqMap again to calculate the total number of deletions needed for the current frequency.\n- For each frequency j:\n- If j is less than freq, add j to curr. Otherwise, add the maximum of 0 and the difference between j, k, and freq to curr.\n7) Update the minimum number of deletions needed (min) by taking the minimum of the current number of deletions (curr) and the current minimum (min).\n8) Finally, return the minimum number of deletions needed.\n\n**Dry Run:\nInput: word = "aabcaba", k = 0\nOutput: 3\n**\n1) Initialize an empty HashMap freqMap to store the frequency of each character.\n2) Iterate through each character in the word "aabcaba":\n3) After this loop, freqMap will contain the frequencies: {\'a\':4,\'b\':2,\'c\':1}.\n4) Initialize min to Integer.MAX_VALUE\n5) Iterate over the values of freqMap\n   For the first iteration, freq is 4.\n   Initialize curr=0.\n   Inner loop: For each frequency j in countMap.values():\n   For \'a\' (freq = 4), j is 4, then curr += Math.max(0, 4 - 0 - 4) = 0+0 = 0\n   For \'b\' (freq = 4), j is 2, j<freq then curr += j =  2+0 = 2\n   For \'c\' (freq = 4), j is 1, j<freq then curr += j = 1+2 = 3\n   Min= Math.min(Integer.MAX_VALUE,curr) = 3\n\n   For \'a\' (freq = 2) , j is 4, then curr + = Math.max(0, 4-0-2)=2+0=2\n   For \'b\' (freq = 2) , j is 2, then curr + = Math.max(0, 2-0-2)=0+2=2\n   For \'c\' (freq = 2), j is 1, j<freq then curr += 1+2 =3\n   Min= Math.min(Integer.MAX_VALUE,curr)= Math.min(3,3)= 3\n   return 3\n\n# Complexity\n- Time complexity: O(n), first for loop iterates through each character in the input word and constructs the frequency map which takes O(n) time and second for loop iterates through the values of the frequency map which takes O(n) time. Total: O(n) time\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n), In the worst case, if all characters in the input word are unique, the space complexity is O(n), where n is the length of the input word\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int minimumDeletions(String word, int k) {\n        Map<Character, Integer> freqMap = new HashMap<>();\n        for (char c : word.toCharArray()) {\n            freqMap.put(c, freqMap.getOrDefault(c, 0) + 1);\n        }\n        int min = Integer.MAX_VALUE;\n        for (int freq : freqMap.values()) {\n            int curr = 0;\n            for (int j : freqMap.values()) {\n                curr += j < freq ? j : Math.max(0, j - k - freq);\n                System.out.println(curr);\n            }\n            min = Math.min(min, curr);\n            System.out.println("Minimum: "+min);\n        }\n        return min;\n    }\n}\n\n// 1) Initialize an empty HashMap freqMap to store the frequency of each character.\n// 2) Iterate through each character in the word "aabcaba":\n// 3) After this loop, freqMap will contain the frequencies: {\'a\':4,\'b\':2,\'c\':1}.\n// 4) Initialize min to Integer.MAX_VALUE\n// 5) Iterate over the values of freqMap\n//    For the first iteration, freq is 4.\n//    Initialize curr=0.\n// Inner loop: For each frequency j in countMap.values():\n// For \'a\' (freq = 4), j is 4, then curr += Math.max(0, 4 - 0 - 4) = 0+0 = 0\n// For \'b\' (freq = 4), j is 2, j<freq then curr += j =  2+0 = 2\n// For \'c\' (freq = 4), j is 1, j<freq then curr += j = 1+2 = 3\n// Min= Math.min(Integer.MAX_VALUE,curr) = 3\n\n// For \'a\' (freq = 2) , j is 4, then curr + = Math.max(0, 4-0-2)=2+0=2\n// For \'b\' (freq = 2) , j is 2, then curr + = Math.max(0, 2-0-2)=0+2=2\n// For \'c\' (freq = 2), j is 1, j<freq then curr += 1+2 =3\n// Min= Math.min(Integer.MAX_VALUE,curr)= Math.min(3,3)= 3\n// return 3\n```
['Array', 'Hash Table', 'Java']