Strings: Making Anagrams

Sort by

recency

|

2138 Discussions

|

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

    Python Solution using counter

    from collections import Counter
    
    def makeAnagram(a, b):
        # Write your code here
        a_dict = Counter(a)
        b_dict = Counter(b)
        
        difference_1 = a_dict - b_dict
        difference_2 = b_dict - a_dict
        
        total_removal = sum(difference_1.values()) + sum(difference_2.values())
        
        return total_removal 
        
    
  • + 0 comments

    Java:

        public static int makeAnagram(String a, String b) {
        int numberOfDeletions = 0;
        for (int i = 0; i < a.length(); i++) {
            String currentCharacter = String.valueOf(a.charAt(i));
            if(b.contains(currentCharacter)){
                b = b.replaceFirst(currentCharacter, "");
            } else {
                ++numberOfDeletions;
            }
        }
        return numberOfDeletions + b.length();
        }
    
  • + 0 comments

    C solution

    int deletions_reqd = 0; int len_a = strlen(a); int len_b = strlen(b); int max = 0; int i = 0;

    //printf("Running solution 2...\n");
    int32_t alphabets_a[26] = {0};
    int32_t alphabets_b[26] = {0};
    
    max = ((len_a >= len_b) ? len_a : len_b);
    //printf("max %i, len_a %i, len_b %i\n", max, len_a, len_b);
    
    if(max == 0)
    {
        goto end;
    }
    
    // accumulate repeated characters
    for(i=0; i<max; i++)
    {
        if(i < len_a)
        {
            //printf("array_a, %c, %u\n", a[i], (uint8_t)(a[i] - 'a'));
            alphabets_a[(a[i] - 'a')]++;
        }
    
        if(i < len_b)
        {
            alphabets_b[(b[i] - 'a')]++;
        }
    }        
    
    for(i=0; i<26; i++)
    {
        //printf("%c: %u\n", ('a' + i), (uint8_t) alphabets_a[i]);
        if((alphabets_a[i] > 0) || (alphabets_b[i] > 0))
        {
            if(alphabets_a[i] == alphabets_b[i])
            {
                continue;
            }
            else 
            {
                deletions_reqd += abs((alphabets_a[i] - alphabets_b[i]));
            }
        }
    }
    
  • + 0 comments

    C#

    public static int makeAnagram(string a, string b)
        {
            Dictionary<char, int> aMap = new();
            Dictionary<char, int> bMap = new();
            int charsToDelete = 0;
            
            for(int i = 0; i < a.Length; i++)
            {
                if(!aMap.TryAdd(a[i], 1))
                {
                    aMap[a[i]] += 1;
                }
            }
            
            for(int j = 0; j < b.Length; j++)
            {
                if(!bMap.TryAdd(b[j], 1))
                {
                    bMap[b[j]] += 1;
                }
            }
            
            foreach(char key in aMap.Keys)
            {
                int aCount = aMap[key];
                int bCount = 0;
                bMap.TryGetValue(key, out bCount);
                
                charsToDelete += Math.Abs(aCount - bCount);
            }
            
            foreach(char key in bMap.Keys)
            {
                int bCount = bMap[key];
                int aCount = 0;
                aMap.TryGetValue(key, out aCount);
                
                if(aCount == 0)
                {
                    charsToDelete += Math.Abs(bCount);
                }
                
            }
            
            return charsToDelete;
        }