title: Why the hashset's performance is way faster than list?
body: <p>This problem is from leetcode (<a href="https://leetcode.com/problems/word-ladder/" rel="nofollow noreferrer">https://leetcode.com/problems/word-ladder/</a>)!</p>
  
  <p>Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:</p>
  
  <p>Only one letter can be changed at a time.
  Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
  Note:</p>
  
  <p>Return 0 if there is no such transformation sequence.
  All words have the same length.
  All words contain only lowercase alphabetic characters.
  You may assume no duplicates in the word list.
  You may assume beginWord and endWord are non-empty and are not the same.</p>
  
  <p>This is my code which takes 800 ms to run:</p>
  
  <pre><code>class Solution {
  public int ladderLength(String beginWord, String endWord, List&lt;String&gt; wordList){
      if(!wordList.contains(endWord))
          return 0;
      int ret = 1;
      LinkedList&lt;String&gt; queue = new LinkedList&lt;&gt;();
      Set&lt;String&gt; visited = new HashSet&lt;String&gt;();
      queue.offer(beginWord);
      queue.offer(null);
      while(queue.size() != 1 &amp;&amp; !queue.isEmpty()) {
          String temp = queue.poll();
          if(temp == null){
              ret++;
              queue.offer(null);
              continue;                
          }
          if(temp.equals(endWord)) {
              //System.out.println("succ ret = " + ret);
              return ret;
          }
          for(String word:wordList) {           
              if(diffOf(temp,word) == 1){
                  //System.out.println("offered " + word);
                  //System.out.println("ret =" + ret);
                  if(!visited.contains(word)){
                  visited.add(word);
                  queue.offer(word); 
                  }
              }
          }
      }
      return 0;
  }
  private int diffOf(String s1, String s2) {
      if(s1.length() != s2.length())
          return Integer.MAX_VALUE;
      int dif = 0;
      for(int i=0;i &lt; s1.length();i++) {
          if(s1.charAt(i) != s2.charAt(i))
              dif++;
      }
      return dif;    
  }
  }
  </code></pre>
  
  <p>Here is another code which takes 100ms to run:</p>
  
  <pre><code>class Solution {
  public int ladderLength(String beginWord, String endWord, List&lt;String&gt; wordList) {
      Set&lt;String&gt; set = new HashSet&lt;&gt;(wordList);
      if (!set.contains(endWord)) {
          return 0;
      }
  
      int distance = 1;
      Set&lt;String&gt; current = new HashSet&lt;&gt;();
      current.add(beginWord);
  
      while (!current.contains(endWord)) {
          Set&lt;String&gt; next = new HashSet&lt;&gt;();
  
          for (String str : current) {
              for (int i = 0; i &lt; str.length(); i++) {
                  char[] chars = str.toCharArray();
  
                  for (char c = 'a'; c &lt;= 'z'; c++) {
                      chars[i] = c;
                      String s = new String(chars);
  
                      if (s.equals(endWord)) {
                          return distance + 1;
                      }
  
                      if (set.contains(s)) {
                          next.add(s);
                          set.remove(s);
                      }
                  }
              }
          }
          distance++;
  
          if (next.size() == 0) {
              return 0;
          }
          current = next;
      }
  
      return 0;
  }
  }
  </code></pre>
  
  <p>I think the second code is less efficient, because it test 26 letters for each word. Why is it so fast?</p>