title: intersection of two strings using Java HashSet
body: <p>I am trying to learn Java by doing some assignments from a Stanford class and am having trouble answering this question. </p>
  
  <blockquote>
    <p>boolean stringIntersect(String a, String b, int len): Given 2 strings,
    consider all the substrings within them of length len. Returns true if
    there are any such substrings which appear in both strings. Compute
    this in O(n) time using a HashSet.</p>
  </blockquote>
  
  <p>I can't figure out how to do it using a Hashset because you cannot store repeating characters. So <code>stringIntersect(hoopla, loopla, 5)</code> should return true. </p>
  
  <p>thanks!</p>
  
  <p>Edit: Thanks so much for all your prompt responses. It was helpful to see explanations as well as code. I guess I couldn't see why storing substrings in a hashset would make the algorithm more efficient. I originally had a solution like :</p>
  
  <pre><code>public static boolean stringIntersect(String a, String b, int len) {
      assert (len&gt;=1);
      if (len&gt;a.length() || len&gt;b.length()) return false;
      String s1=new String(),s2=new String();
      if (a.length()&lt;b.length()){
          s1=a;
          s2=b;
      }
      else {
          s1=b;
          s2=a;
      }
      int index = 0;
      while (index&lt;=s1.length()-len){
          if (s2.contains(s1.substring(index,index+len)))return true;
          index++;
      }
      return false;
  }
  </code></pre>