Strings: Making Anagrams

Sort by

recency

|

2142 Discussions

|

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

    Java Solution using int array for 26 English letters:

    public static int makeAnagram(String a, String b) {
        // Since there are only 26 lowercase English letters
        int[] charCount = new int[26]; 
        int deleteCount = 0;
    
        // Count frequency of each character in string 'a'
        for (char c : a.toCharArray()) {
            charCount[c - 'a']++;
        }
    
        // Adjust frequency based on characters in string 'b'
        for (char c : b.toCharArray()) {
            charCount[c - 'a']--;
        }
    
        // Sum up the absolute differences to calculate deletions
        for (int count : charCount) {
            deleteCount += Math.abs(count);
        }
    
        return deleteCount;
    }