title: Finding Minimum Distance Between Words in An Array
body: <blockquote>
    <p>Example:</p>
    
    <p>WordDistanceFinder finder = new
    WordDistanceFinder(Arrays.asList("the", "quick", "brown", "fox",
    "quick"));</p>
    
    <p>assert(finder.distance("fox","the") == 3);</p>
    
    <p>assert(finder.distance("quick", "fox") == 1);</p>
  </blockquote>
  
  <p>I have the following solution, which appears to be O(n), but I'm not sure if there is a better solution out there. Does anyone have any ideas?</p>
  
  <pre><code>String targetString = "fox";
  String targetString2 = "the";
  double minDistance = Double.POSITIVE_INFINITY;
  for(int x = 0; x &lt; strings.length; x++){
      if(strings[x].equals(targetString)){
          for(int y = x; y &lt; strings.length; y++){
              if(strings[y].equals(targetString2))
                  if(minDistance &gt; (y - x))
                      minDistance = y - x;
          }
          for(int y = x; y &gt;=0; y--){
              if(strings[y].equals(targetString2))
                  if(minDistance &gt; (x - y))
                      minDistance = x - y;
          }
      }
  }
  </code></pre>