Sherlock and Anagrams Hackerrank Solution | Codeityweb

Sherlock and Anagrams Hackerrank Solution by Codeityweb 

In this post we are going to discuss the Sherlock and Anagrams Hackerrank Solution in java.

Before looking at the problem statement and the solution lets see some terms that are needed for understanding the questions.

1.What are Anagrams? 

  • Two strings are anagrams of each other if the letters of one string can be rearranged to form the other string.
  • lets say we have two Strings a="codi" ,b="oicd".Here you can see that the letter number of characters in both the strings are same.
  • a:{c-1,o-1,d-1,i-1},b:{c-1,o-1,d-1,i-1}. So here as you can see the characters interchanges their position but the length of the string and the sum of all characters are same thus both are anagrams of each other.
  • Now lets say x="web",y="webe", x:{w-1,e-1,b-1},y:{w-1,e-2,b-1}.You can see that neither the length of the strings are same nor the total character count is same thus they are not anagram of each other.

The Problem statement is as follows:


Given a string, find the number of pairs of substrings of the string that are anagrams of each other. 

 

Approach and Algorithm for Sherlock and Anagrams Hackerrank Solution:

  • As the question asks to find the count of substrings that are anagrams of each others.
  • So we need to perform two tasks 1) Get All Substrings of the String. 2)Check if the substring are anagram or not.
  • To Find all substring we need to loop through the string and find the substrings using java's inbuild function substring(x,y) ,where x and y are indices of the character of the string.
  • To Check if the strings are anagram of each other , as we see above that anagram means the count of the characters are same in both strings.
  • So we need to count the frequency of each character in the strings ,and then check if both the count is same or not if it is same we return true else we return false.
  • Finally we loop through the substrings and check if they are anagrams or not and increment count if they are anagrams.

 

Check if Two String are anagrams in java:



public static boolean isanagram(String a,String b)
{
    if(a.length()!=b.length())return false;
    int[] freq=new int[26];
    for(int i=0;i<a.length();i++)
    {
        freq[a.charAt(i)-'a']++;
        freq[b.charAt(i)-'a']--;
       }
    for(int i:freq)
    {
        if(i!=0)return false;
    }
    return true;
}


Sherlock and Anagrams Hackerrank Solution Java:

static int sherlockAndAnagrams(String s) {
    int count=0;
    ArrayList<String> al=new ArrayList<>();
    for(int i=0;i<s.length();i++)
     {
        for(int j=i+1;j<=s.length();j++)
        {
        al.add(s.substring(i,j));
        }
       }
    for(int i=0;i<al.size();i++)
    {
        for(int j=i+1;j<al.size();j++)
        {
        if(isanagram(al.get(i),al.get(j)))
        {
            count++;
            }
        }
    }

        return count;

}

 Output:

Sherlock and Anagrams Hackerrank Solution

 

Related Posts:

 
Thank you for Reading!
If some questions are missing you can get in touch by using contact form or by commenting on the posts.
 

Post a Comment

0 Comments