title: Find the number of unordered anagramic pairs of substrings
body: <p>I am trying to solve the following question:
  <a href="https://www.hackerrank.com/challenges/sherlock-and-anagrams" rel="nofollow">https://www.hackerrank.com/challenges/sherlock-and-anagrams</a></p>
  
  <p>This is my code</p>
  
  <pre><code>import java.util.*;
  public class Solution {
  
  public static boolean check(String s1,String s2)
  {
  
      int [] count1 = new int[26];
      for( int i = 0; i &lt; s1.length(); i++ )
      {
          char ch1 = s1.charAt(i);
          count1[ch1-'a']++;
      }
  
      int [] count2 = new int[26];
      for( int i = 0; i &lt; s2.length(); i++ )
      {
          char ch2 = s2.charAt(i);
          count2[ch2-'a']++;
      }
  
      int count =0;
      for(int j=0;j&lt;26;j++)
      {
          count = count + Math.abs(count1[j]-count2[j]);
      }
  
      if(count ==0)
              return true;
      else return false;
  }
  public static void main(String[] args) {
  
      String s,sub;
      int i,c,len;
      List&lt;String&gt; all = new ArrayList&lt;&gt;();
  
      Scanner in = new Scanner(System.in);
      int t = Integer.parseInt(in.nextLine());
  
        while((t--)&gt;0)
      {
            s  = in.nextLine();
            len = s.length();   
         for( c = 0 ; c &lt; len ; c++ )
         {
             for( i = 1 ; i &lt;= len - c ; i++ )
            {
               sub = s.substring(c, c+i);
              all.add(sub);
            }
         }
  
        String[] arr = new String[all.size()];
        for( i = 0; i &lt; all.size(); i++) 
                arr[i] = all.get(i);
  
            int l=0;
            for (int m=0;m&lt;arr.length;m++)
            {
                for(int n=m+1;n&lt;arr.length;n++)
                 {
                    if(check(arr[m],arr[n]))
                           l++;
                }
            }
  
            System.out.println(l);all.clear();
      }
  
  }
  }
  </code></pre>
  
  <p>My code worked for few test cases which have small strings but failed to work if string size is too big </p>
  
  <p>Sample input</p>
  
  <pre><code>5
  ifailugtyovhdfuhdouarjsnrbfpvmupwjjjfiwneogwnoegnoegneognoewhrlkpekxxnebfrwibylcvkfealgonjkzw
  gffryqktmwocejbrexfidpjfgrrkpowoxwggxaknmltjcpazgtnakcfbveieivoenwvpnoevvneocogzatyskqjyorcftw
  uqlzvuzgkwhkkrrfpwarkckansgabfclzgnumdrojexnofeqjnqnxwidhbvbenevun9evnnv9euxxhfwargwkikjq
  sygjxynvofnvirarcoacwnhxyqlrviikfuiuotifznqmzpjrxycnqkeibvibvewioebvitkryutpqvbgbgthfges
  mkenscyiamnwlpxytkndjsygifmqlqibxxqlauxamfviftquntvkwppxrzuncyenavebiobeviobeiobeibvcfivtigv
  </code></pre>
  
  <p>My Output </p>
  
  <pre><code>4s : Terminated due to timeout
  </code></pre>
  
  <p>is there any better way to solve it or to change the existing code so that executed time is within 4mins</p>