Strings: Making Anagrams

Sort by

recency

|

2143 Discussions

|

  • + 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;
    }
    
  • + 0 comments

    my python solution -

    def makeAnagram(a, b):

    # Write your code here
    start = time.time()
    count=0
    dict_a = {}
    dict_b = {}
    for i in a:
        if i in dict_a:
            dict_a[i]+=1
        else:
            dict_a[i]=1
    
    for i in b:
        if i in dict_b:
            dict_b[i]+=1
        else:
            dict_b[i]=1
    
    temp_list = list(dict_a.keys()).copy()
    
    for i in temp_list:
    
        if i in dict_b:
            count += abs(dict_a[i]-dict_b[i])
            dict_a.pop(i)
            dict_b.pop(i)
        else:
            count+=dict_a[i]
            dict_a.pop(i)
    
    count=count+sum(dict_b.values())
    print(f'total time: {time.time()-start:.8f}')
    return count
    
  • + 0 comments
    function makeAnagram(a: string, b: string): number {
        // Write your code here
        let dictA = new Map<string, number>();
        let dictB = new Map<string, number>();
        for(let ch of a){
            if (dictA.has(ch)){
                dictA.set(ch, dictA.get(ch)+1)
            }else{
                dictA.set(ch, 1)
            }
        }
        for(let ch of b){
            if (dictB.has(ch)){
                dictB.set(ch, dictB.get(ch)+1)
            }else{
                dictB.set(ch, 1)
            }
        }
        let deleteDiff = 0;
        dictA.forEach((v, k) =>{
            if (dictB.has(k)){
                deleteDiff += Math.abs(v - dictB.get(k));
            }else{
                deleteDiff +=v
            }
        })
        dictB.forEach((v, k) =>{
            if (!dictA.has(k)){
                deleteDiff += v;
            }
        })
        
        return deleteDiff
    }
    
  • + 0 comments
        public static int makeAnagram(String a, String b) {
            int[] arrs = new int['z' + 1];
            
            // Traverse a, b
            for (char c : a.toCharArray()) {
                arrs[c]++;
            }
            for (char c : b.toCharArray()) {
                arrs[c]--;
            }
            
            // Extract the additional values
            return Arrays.stream(arrs) //
                            .reduce(0, (v1, v2) -> v1 + Math.abs(v2));
        }
    
  • + 0 comments

    Scala

    def makeAnagram(a: String, b: String): Int = {
            val anagram = a intersect b
            (a.length - anagram.length) + (b.length - anagram.length)
        }