Strings: Making Anagrams

  • + 0 comments

    Well for this one Very simple solution is to add the Char count in hasmap and remove which is there on the first one.

    public static int makeAnagram(String a, String b) {
    // Write your code here
            int deletecount = 0;
            HashMap<Character,Integer> checkFreq = new HashMap<>();
    
             for (char c : a.toCharArray()) {
                    checkFreq.put(c, checkFreq.getOrDefault(c, 0) + 1);
            }
    
            // Subtract frequency using second string
            for (char c : b.toCharArray()) {
                    checkFreq.put(c, checkFreq.getOrDefault(c, 0) - 1);
            }
    
            for(int count : checkFreq.values()){
                    deletecount += Math.abs(count);
            }
    
            return deletecount;
    }