Anagram HackerRank Solution

In this Anagram HackerRank solution, Two words are anagrams of one another if their letters can be rearranged to form the other word.

Given a string, split it into two contiguous substrings of equal length. Determine the minimum number of characters to change to make the two substrings into anagrams of one another.

Example

Break  into two parts: ‘abc’ and ‘cde’. Note that all letters have been used, the substrings are contiguous and their lengths are equal. Now you can change ‘a’ and ‘b’ in the first substring to ‘d’ and ‘e’ to have ‘dec’ and ‘cde’ which are anagrams. Two changes were necessary.

Function Description

Complete the anagram function in the editor below.

anagram has the following parameter(s):

  • string s: a string

Returns

  • int: the minimum number of characters to change or -1.

Input Format

The first line will contain an integer, , the number of test cases.
Each test case will contain a string .

Constraints

  •  consists only of characters in the range ascii[a-z].

Sample Input

6
aaabbb
ab
abc
mnop
xyyx
xaxbbbxx

Sample Output

3
1
-1
2
0
1

Explanation

Test Case #01: We split  into two strings =’aaa’ and =’bbb’. We have to replace all three characters from the first string with ‘b’ to make the strings anagrams.

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 anagrams of one another.

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

Test Case #05:  and  are already anagrams of one another.

Test Case #06: Here S1 = “xaxb” and S2 = “bbxx”. You must replace ‘a’ from S1 with ‘b’ so that S1 = “xbxb”.

Anagram HackerRank Solution

I will Provide solution in Multiple programming languages for you. If you are not able to find the code in required language then please share in comments so that our team can help you.

Problem Solution in Python

def anagram(s):
    if len(s) % 2 != 0: return -1
    s1 = list(s[:len(s)//2])    
    s2 = list(s[len(s)//2:])  
    for char in s2:
        try:
            index = s1.index(char)
            s1.pop(index)
        except: continue
    return len(s1)

Problem Solution in C++

int anagram(string s) {
    if(s.size() % 2 == 1) return -1;
    map<char, int> mp;
    int ans = 0;
    for(int i = 0; i < s.size() / 2; i++) mp[s[i]]++;
    for(int i = s.size() / 2; i < s.size(); i++){
        if(mp[s[i]] != 0) mp[s[i]]--;
        else ans++;
    }
    return ans;
}

Problem Solution in Java

 public static int anagram(String s) {
    // Write your code here
     if(s.length()%2!=0) return -1;
     
     int mid=s.length()/2;
     String str=s.substring(0,mid);
     String str2=s.substring(mid);
     
     for(int i=0;i<str.length();i++){
         char ch=str.charAt(i);
         if(str2.contains(ch+"")){
            str2=str2.replaceFirst(ch+"", "");
         }
     }
     return str2.length();
    }

Problem Solution in JavaScript

function anagram(s) {
    // Write your code here
    if(s.length%2==1)
        return -1;
    let halfLength=s.length/2,count=0;


    let left=s.slice(0,halfLength);
    let right=s.slice(halfLength);
 
   
    for(let i=0;i<halfLength;i++){
        left=left.replace(right[i],'')
    }
   
    return left.length;


}

Solve original Problem on HackerRank here. Checkout more HackerRank Problems

Leave a Comment