Making Anagrams Hackerrank problem solution

Making Anagrams Hackerrank

Alice is taking a cryptography class and finding anagrams to be very useful. We consider two strings to be anagrams of each other if the first string’s letters can be rearranged to form the second string. In other words, both strings must contain the same exact letters in the same exact frequency, For example, bacdc and dcbac are anagrams, but bacdc and dcbad are not.

Alice decides on an encryption scheme involving two large strings where encryption is dependent on the minimum number of character deletions required to make the two strings anagrams. Can you help her find this number?

Given two strings, a and b, that may or may not be of the same length, determine the minimum number of character deletions required to make a and b anagrams. Any characters can be deleted from either of the strings.

See problem at “”
Input Format:- The first line contains a single string, a.
The second line contains a single string, b.


It is guaranteed that a and b consist of lowercase English letters.

Output Format

Print a single integer denoting the number of characters which must be deleted to make the two strings anagrams of each other.

Sample Input

Sample Output


We delete the following characters from our two strings to turn them into anagrams of each other:

Remove d and e from cde to get c.
Remove a and b from abc to get c.

We had to delete 4 characters to make both strings anagrams, so we print 4 on a new line.


Two strings, a and b, will be anagrams of one another if they share all of the same characters and each character has the same frequency in both strings. Keep a count array for each of them that stores the number of occurrences of each of character. Suppose character c occurs x times in string a and y times in string b; in this case, we’ll have to perform max(x-y)-min(x-y) deletions for all of the characters.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    char A[10000];
    char B[10000];

    scanf("%s", A);
    scanf("%s", B);

    int letterA[26] = {0};
    int letterB[26] = {0};

    for (int i = 0; i < strlen(A); i++) {
        char c = A[i];
        letterA[c - 'a']++;

    for (int i = 0; i < strlen(B); i++) {
        char c = B[i];
        letterB[c - 'a']++;    

    int count = 0;
    for (int i = 0; i < 26; i++) {
        count += abs(letterB[i] - letterA[i]);

    printf("%d", count);

from collections import *    
a = Counter(raw_input())
b = Counter(raw_input())
c = a - b
d = b - a
e = c + d
print len(list(e.elements()))
return 0;

using namespace std;

int main() {
    string str1,str2;

    int A[26],B[26],i;
    for(i=0 ; i< 26 ; i++)
        A[i] = B[i] = 0;
    for(i = 0 ; i< str1.length() ; i++)
        A[(int)(str1[i] - 'a')]++;
    for(i = 0 ; i< str2.length() ; i++)
        B[(int)(str2[i] - 'a')]++;	
    int outp = 0;
    for(i=0 ; i< 26 ; i++)
        outp = outp + A[i] + B[i] - 2*min(A[i],B[i]);
    return 0;

A web developer(Front end and Back end), and DBA at Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me

Leave a reply:

Your email address will not be published.