title: Java method Optimization
body: <p>I am writing some java code 
  i wrote a method and for the test input it is taking morethan 5sec's to execute
  I really really wanna keep it less than 5sec
  can anyone suggest me how i can optimize my method</p>
  
  <pre><code>private static String getShortestSub(ArrayList&lt;String&gt; paraWordsList,
          ArrayList&lt;Integer&gt; paraWordsIndexes,
          ArrayList&lt;Integer&gt; lowFreqIndexes) {
  
      long d = System.currentTimeMillis();
      // Finding the substring
      int startTxtIndex = 0, endTxtIndex = 0;
      int tempLength = paraWordsList.size();
      for (int i = 0; i &lt; lowFreqIndexes.size(); i++) 
      {
          int point = lowFreqIndexes.get(i), startIndex = 0;
          HashSet&lt;String&gt; frame = new HashSet&lt;String&gt;();
  
  
          // index is the indexes of paraWordsIndexes
          startIndex =paraWordsIndexes.indexOf(point);
          for (int index = paraWordsIndexes.indexOf(point); index &gt;= 0; index--) 
          {
              if (frame.add(paraWordsList.get(paraWordsIndexes.get(index))))
              {
                  startIndex = index;
                  if (frame.size() == K
                          || (paraWordsIndexes.get(startIndex) - point) &gt;= tempLength) 
                      index = -1;                 
              }
          }
          frame.clear();
  
          for (int start = startIndex, index = startIndex; start &lt;= paraWordsIndexes
                  .indexOf(point) &amp;&amp; index &lt; paraWordsIndexes.size(); index++) 
          {
              int tempStart = paraWordsIndexes.get(start), tempEnd = paraWordsIndexes.get(start);
              int currIndex = paraWordsIndexes.get(index);
              String word = paraWordsList.get(currIndex);
              if ((tempStart - point) &gt;= tempLength)          break;
              if ((tempStart - currIndex) &gt;= tempLength)      break;
                      frame.add(word);
              if (frame.size() == K) 
              {
                  tempEnd = currIndex;
                  int newLength;
                  if ((newLength = tempEnd - tempStart) &gt; 0)
                      if (tempLength &gt; newLength) 
                      {
                          tempLength = newLength;
                          startTxtIndex = tempStart;
                          endTxtIndex = tempEnd;
                          if (K == (tempLength+1)) {
                              i = lowFreqIndexes.size();
                              break;
                          }
                      }
                  frame.clear();
                  tempStart = paraWordsList.size();
                  start++;
                  index = start - 1;
              }
          }
          frame.clear();
          System.out.println(System.currentTimeMillis() - d);
      }
  
      String[] result = paraText.split(" ");
      ArrayList&lt;String&gt; actualParaWordsList = new ArrayList&lt;String&gt;(
              Arrays.asList(result));
  
      return textCleanup(actualParaWordsList.subList(startTxtIndex,
              endTxtIndex + 1).toString());
  }
  </code></pre>