Making Anagrams -Hackerrank Solutions

Making Two Strings Anagrams -Hackerrank Solutions

 

Question:

Given two strings a and string b , you have to delete/add characters from the strings to make them anagram of each other.

So before starting lets see what anagrams are. Consider a string "abc" the anagram of the string are "acb","bca","cab" etc.. That is that number of string that can be formed by rearranging the characters of the string without any addition or deletion of character.

In short if the string has {a=2,b=4,c=3} characters then the anagram will also have the same number of character i.e {a=2,b=4,c=3} but they can be in other order.

So in this post we have discussed Making Anagrams Hackerrank Solution

So lets start the algorithm for this:

 

Explanation:

  • As we have seen we need the count of the characters , so we will make a frequency array to store the count of each character in the array.
  • This will be for both the string since we have to make them anagram of each other.
  • Now the next step will be to find the total number of characters that are in string a and not in string b and vice versa
  • For that since we have the count of each character we simply need to take difference of the characters in the strings to count the total number of characters that are missing in either of the string.
  • Finally return the count of the numbers

We will understand it through an example:

    String x="abc"

    String y="cde"

  • So when we take the count of each character we get x_count={a=1,b=1,c=1}, y_count={c=1,d=1,e=1}

Here we have completed first step now we need to count the total  missing characters in the strings . 

  •  a is 1 in string x but 0 in string y so the count becomes 1.
  •  b is 1 in string x but 0 in string y so the count becomes 2.
  • c is 1 in string x and 1 in string y so no need to make changes count remains 2.
  • d is 1 in string y but 0 in string x so count becomes 3.
  • finally e is 1 in y but 0 in x so count becomes 4.
 
Therefore their is total 4 characters difference in  both the string thus we need 4 characters addition or deletion to make the string anagram
  • If we add 4 characters the string becomes x= "abcde"  y="abcde" here we add d,e to x and a,b to y So that they becomes anagram
  • if we delete 4 characters the string becomes x="c" , y="c" here we delete 2 characters i.e a,b from x and 2 characters  d,e from y So they becomes anagram.
 

Making Anagram Code:

 

import java.util.*;

public class Solution {

static int makeAnagram(String a, String b) {
int frequency_of_a[]=new int[26];
int frequency_of_b[]=new int[26];
int count=0;

for(int i=0;i<a.length();i++)
{
    frequency_of_a[a.charAt(i)-'a']++;
}
for(int i=0;i<b.length();i++)
{
    frequency_of_b[b.charAt(i)-'a']++;
}

for(int i=0;i<26;i++)
{
    if(Math.abs(frequency_of_a[i]-frequency_of_b[i])>0)
    {
        count+=Math.abs(frequency_of_a[i]-frequency_of_b[i]);
    }
}
return count;
 }

 public static void main(String []args)
   {
       Scanner sc=new Scanner(System.in);
       String a=sc.next();
       String b=sc.next();
       int result=makeAnagram(a,b);
       System.out.println(result);

   }
   
}


Post a Comment

0 Comments