title: An algorithm to search for words in an array of letters
body: <p>So I have an Array, and I have to search for words  </p>
  
  <p>The Array:</p>
  
  <pre><code>        0   1   2   3   4   5   6   7   8   9   10  11
  text    g   t   c   a   n   d   l   e   t   j   a   q
  </code></pre>
  
  <p>The Keys:</p>
  
  <pre><code>2 can
  2 candle
  3 a
  3 an
  3 and
  6 let
  10 a
  </code></pre>
  
  <p>The number is an offset from the start of the array being searched, and the string is a word from the dictionary that was found at that offset. Note that several words can start at the same offset, and the same word can be found in multiple places. Note also that words can overlap.</p>
  
  <p>This is the code I have written:</p>
  
  <pre><code>public ArrayList&lt;Location&gt; findWords(String[] dictionary, String text) {
      int keyLength = text.length();
      int dtLength = dictionary.length;
  
      ArrayList&lt;Location&gt; results;
      results = new ArrayList&lt;&gt;();
  
      for (int k = 0; k &lt; keyLength; k++) {
          for (int d = 0; d &lt; dtLength; d++) {
              if (dthasKey(dictionary[d], text, k)) {
                  Location loc = new Location(k, dictionary[d]);
                  results.add(loc);
              }
          }
      }
      return results;
  }
  
  private boolean dthasKey(String key, String text, int pos) {
      for (int i = 0; i &lt; key.length(); i++) {
          if (key.length() &gt;= text.length() - pos)
              return false;
          while (key.charAt(i) != text.charAt(pos + i)) {
              return false;
          }
      }
      return true;
  }
  </code></pre>
  
  <p>I was wondering if there is a better solution to this problem. If you guys may also provide a worst time complexity, that would be great. The one I have written is:
          <code>O(k*n*m)</code>
  where m is the size of the
  dictionary, n is the size of the text, and k is the length of
  the longest word.</p>