title: Error in finding count of anagrams of a large substring
body: <p>I am trying to check and count of anagrams pairs for every sub-strings of a input string.</p>
  
  <p>For example, if the input string is <code>mom</code>, then anagram pairs are <code>m,m</code> and <code>mo,om</code>.</p>
  
  <p>The code works good and passed for the three strings test cases. But the code gets terminated due to timeout constraints for the long input strings like : </p>
  
  <pre><code>ifailuhkqqhucpoltgtyovarjsnrbfpvmupwjjjfiwwhrlkpekxxnebfrwibylcvkfealgonjkzwlyfhhkefuvgndgdnbelgruel
  </code></pre>
  
  <p>I tried and researched for the issue but I am struck with this error. Ccan you guys kindly help me to resolve the issue with your suggestions?</p>
  
  <p>Providing code below:</p>
  
  <pre><code>import java.io.*;
  import java.math.*;
  import java.security.*;
  import java.text.*;
  import java.util.*;
  import java.util.concurrent.*;
  import java.util.regex.*;
  
  public class Solution {
  
      // Complete the sherlockAndAnagrams function below.
      static int sherlockAndAnagrams(String s) {
  
          for(int i=0; i&lt;s.length(); i++){
              for(int j=i+1; j&lt;=s.length(); j++){
                  sArray[index] = s.substring(i,j);
                  index++;
                  //System.out.println(s.substring(i,j));
                  //System.out.println(Arrays.toString(sArray));
              }
          }
          for(int i=0; i&lt;sArray.length; i++){
              for(int j=i; j&lt; sArray.length; j++){
                  if(i != j){
                  if(null == sArray[i])
                  break;
                  if(null == sArray[j])
                  break;
                  char[] sArray1 = sArray[i].toCharArray();
                  char[] sArray2 = sArray[j].toCharArray();
                  //System.out.println(sArray1);
                  //System.out.println(sArray2);
                  //int index_str = 0;
                  Hashtable&lt;Character, Integer&gt;sHash1 = new Hashtable&lt;Character, Integer&gt;();
                  Hashtable&lt;Character, Integer&gt;sHash2 = new Hashtable&lt;Character, Integer&gt;();
                  for (int k = 0; k &lt; sArray1.length; k++) { 
  
                      if (sHash1.get(sArray1[k]) == null) { 
  
                          sHash1.put(sArray1[k], 1); 
                      } 
                      else { 
                          Integer c = (int)sHash1.get(sArray1[k]); 
                          sHash1.put(sArray1[k], ++c); 
                      } 
                  } 
  
                  // Mapping second String 
                  for (int l = 0; l &lt; sArray2.length; l++) { 
  
                      if (sHash2.get(sArray2[l]) == null) 
                          sHash2.put(sArray2[l], 1); 
                      else { 
  
                          Integer d = (int)sHash2.get(sArray2[l]); 
                          sHash2.put(sArray2[l], ++d); 
                      } 
                  } 
  
                  if(sHash1.equals(sHash2)){
                      count++;
                  }
                  }
              }
          }
  
          }*/
          //System.out.println(sHash);
          return count;
      }
  
      private static final Scanner scanner = new Scanner(System.in);
  
      public static void main(String[] args) throws IOException {
          BufferedWriter bufferedWriter  = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
  
          int q = scanner.nextInt();
          scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
  
          for (int qItr = 0; qItr &lt; q; qItr++) {
              String s = scanner.nextLine();
  
              int result = sherlockAndAnagrams(s);
  
              bufferedWriter.write(String.valueOf(result));
              bufferedWriter.newLine();
          }
  
          bufferedWriter.close();
  
          scanner.close();
      }
  }
  </code></pre>