Strings: Making Anagrams

Sort by

recency

|

2147 Discussions

|

  • + 0 comments
    def makeAnagram(a, b) -> int:
        # Write your code here
        ca, cb = Counter(a), Counter(b)
        diff = (ca|cb) - (ca&cb)
        return sum(diff.values())
    
  • + 0 comments

    Python solution

    def makeAnagram(a, b):
        counter_a = Counter(a)
        counter_b = Counter(b)
        unique_char = set(a).union(set(b))
        
        return sum(abs(counter_a[char] - counter_b[char]) for char in unique_char)
    
  • + 0 comments
                char[] aArray = a.ToCharArray();
                char[] bArray = b.ToCharArray();  
                Array.Sort(aArray);
                Array.Sort(bArray);  
                int i = 0, j = 0;
                int count = 0;
                while (i < aArray.Length && j < bArray.Length)
                {
                     if (aArray[i] == bArray[j])
                     {
                            i++;
                            j++;
                     }
                     else if (aArray[i] < bArray[j])
                     {
                            i++;
                            count++;
                     }
                    else
                 {
                         j++;
                         count++;
                 }
            }
            if(i < aArray.Length)
            {
                 count += aArray.Length - i;
            }
            if(j < bArray.Length)
            {
                 count += bArray.Length - j;
            }
    
            return count;
    
  • + 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;
    }
    
  • + 0 comments

    Java15 solution that passes all test cases. Three HashMaps and a dream...

    public static int makeAnagram(String a, String b) {
        int count = 0;
        
        HashMap<Character, Integer> aCharFreq = new HashMap<Character, Integer>();
        HashMap<Character, Integer> bCharFreq = new HashMap<Character, Integer>();
        HashMap<Character, Integer> toDelCharFreq = new HashMap<Character, Integer>();
        
        for (int i = 0; i < a.length(); i++) { //Get the character frequencies for string a.
            char c = a.charAt(i);
            if (aCharFreq.containsKey(c)) aCharFreq.replace(c, aCharFreq.get(c) + 1);
            else aCharFreq.put(c, 1);
        }
        
        for (int i = 0; i < b.length(); i++) { //Get the character frequencies for string b.
            char d = b.charAt(i);
            if (bCharFreq.containsKey(d)) bCharFreq.replace(d, bCharFreq.get(d) + 1);
            else bCharFreq.put(d, 1);
        }
        
        for (char keyA : aCharFreq.keySet()) { //Now check the character frequencies for string a.
            if (bCharFreq.containsKey(keyA)) toDelCharFreq.put(keyA,Math.abs(bCharFreq.get(keyA) - aCharFreq.get(keyA))); //If b contains the character in a that we're looking at, add the difference between the number of times that character shows up in b and a.
            else toDelCharFreq.put(keyA,aCharFreq.get(keyA)); //Add every character that shows up in a but not b.
        }
                
        for (char keyB : bCharFreq.keySet()) { //Then check the character frequencies for string b.
            if (aCharFreq.containsKey(keyB) && toDelCharFreq.get(keyB) == 0) toDelCharFreq.remove(keyB); //If a and b have an equal number of instances of a character, that character will be part of the anagram set and must be removed from the target character frequencies.
            else if (!aCharFreq.containsKey(keyB)) toDelCharFreq.put(keyB,bCharFreq.get(keyB)); //Add every character that shows up in b but not a.
        }
        
        for (char key : toDelCharFreq.keySet()) count += toDelCharFreq.get(key); //Finally, add up the frequencies of all characters remaining to get the retur value.
        
        return count;
    }