Anagram Hackerrank problem solution

Anagram Hackerrank

Two words are said to be anagrams of each other if the letters from one word can be rearranged to form the other word.

He chooses strings S1 and S2 in such a way that |len(S1)-len(S2)|<=1.

See problem at:-


Your task is to help him find the minimum number of characters of the first string he needs to change to enable him to make it an anagram of the second string.

Note: A word x is an anagram of another word y if we can produce y by rearranging the letters of x.

Input Format:-

The first line will contain an integer, T, representing the number of test cases. Each test case will contain a string having length len(S1)+len(S2), which will be the concatenation of both the strings described above in the problem.The given string will contain characters from a to z.



Output Format:-

An integer corresponding to each test case is printed in a different line, i.e. the number of changes required for each test case. Print -1 if it is not possible.

Sample Input:-


Sample Output:-



Test Case #01: We have to replace all three characters from the first string to make both of strings anagram. Here, S1 = “aaa” and S2 = “bbb”. So the solution is to replace all character ‘a’ in the string ‘a’ with character ‘b’.

Test Case #02: You have to replace ‘a’ with ‘b’, which will generate “bb”.

Test Case #03: It is not possible for two strings of unequal length to be the anagram for each other.

Test Case #04: We have to replace both the characters of the first string (“mn”) to make it an anagram of another one.

Test Case #05: Anagrams.

Test Case #06: Here S1 = “xaxb” and S2 = “bbxx”. He had to replace ‘a’ from S1 with ‘b’ so that S1 = “xbxb” and we can rearrange its letter to “bbxx” in order to get S2.


Lets us consider that the length of the given string is X. If X is an odd number, we can’t break it into two strings of equal length. As we know, two strings having unequal length cannot be anagrams. So, the required output will be “-1”.


Now suppose the length of the string is an even number, we can change any character of the string to any alphabet. So, the only difference between the number of each character matters. We can count the difference of a number of occurrences of each character in the first half and the second half i.e., from ‘a’ to ‘z’.

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

int main() {
int t;
scanf("%d", &t);
while (t--) {
    char s[10000];
    scanf("%s", s);
    int len = strlen(s);
    if (len % 2 == 1) {

    int letter[26] = {0};
    int count = 0;
    for (int i = 0; i < len/2; i++) {
        char c = s[i];
        letter[c - 'a']++;
    for (int i = len/2; i < len; i++) {
        char c = s[i];
        letter[c - 'a']--;

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

    printf("%d\n", count/2);
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
from collections import Counter
for _ in range(input()):
    s = raw_input()
    if len(s)%2 == 1:
        print "-1"
    temp = Counter(s[0:len(s)/2]) - Counter(s[len(s)/2:])
    print sum(temp.values())


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.