title: Suggestion required for optimization of word search requirement
body: <p>I have written code for word search problem where we need to search and get the count of occurrences of particular word in N*N grid. I need some suggestion to improve this code, for better performance.</p>
  
  <p>Words can be search horizontally, vertically and diagonally in both directions </p>
  
  <p>code is :</p>
  
  <pre><code>Scanner sc = new Scanner(System.in);
  String size = sc.nextLine();
  String finalArr[][] = new String[Integer.parseInt(size)][Integer.parseInt(size)];
  
  int count = 0;
  String diagonal1 = "", diagonal2 = "";
  
  String arr[] = sc.nextLine().split(",");
  
  // initializing array
  for (int i = 0; i &lt; arr.length; i++ ) {
  
      String b[]= arr[i].split("#");
      for (int j = 0; j &lt; arr.length; j++) {
  
          finalArr[i][j] = b[j];
      }
  
  }
  
  StringBuilder searchWord = new StringBuilder(sc.nextLine());
  // Loop for horizontal and vertical count
  for (int i = 0; i &lt; finalArr.length; i++) {
  
      String horizontal = "";
      String vertical = "";
  
      for (int j = 0; j &lt; finalArr.length; j++) 
      {           
          horizontal += finalArr[i][j];   
          vertical += finalArr[j][i];
      }
  
      if ( horizontal.contains(searchWord) ||
           horizontal.contains(searchWord.reverse() ) )
          count++;
  
      if ( vertical.contains(searchWord) ||
           vertical.contains(searchWord.reverse() ) )
          count++;
  }
  
  //loop for one diagonal count
  for (int i = 0; i &lt; finalArr.length; i++ ) {
          diagonal1 += finalArr[i][i];
  }
  
  //loop for another diagonal count
  for ( int i = 0, j = finalArr[i].length - 1;
        i &lt; finalArr.length;
        i++, j--) {
  
      diagonal2+= finalArr[i][j];
  }
  
  if ( diagonal1.contains(searchWord) ||
       diagonal1.contains(searchWord.reverse() ) )
      count++;
  
  if ( diagonal2.contains(searchWord) ||
       diagonal2.contains(searchWord.reverse() ) )
      count++;
  
  System.out.println(count);
  
  sc.close();
  </code></pre>
  
  <p>I want to reduce number of loops. Apart from that can someone suggest me how to handle overlapping scenario. For example in 5*5 first row value is <code>ASKSA</code> and word to be search is <code>ASK</code>. In this case <code>K</code> is the overlapping character.</p>