title: Comparison of these two algorithms?
body: <p>So I'm presented with a problem that states. "Determine if a string contains all unique characters"</p>
  
  <p>So I wrote up this solution that adds each character to a set, but if the character already exists it returns false.</p>
  
  <pre><code>private static boolean allUniqueCharacters(String s) {
  
      Set&lt;Character&gt; charSet = new HashSet&lt;Character&gt;();
      for (int i = 0; i &lt; s.length(); i++) {
          char currentChar = s.charAt(i);
          if (!charSet.contains(currentChar)) {
              charSet.add(currentChar);
  
          } else {
              return false;
          }
  
      }
      return true;
  
  }
  </code></pre>
  
  <p>According to the book I am reading this is the "optimal solution"</p>
  
  <pre><code>public static boolean isUniqueChars2(String str) {
      if (str.length() &gt; 128)
          return false;
  
      boolean[] char_set = new boolean[128];
  
      for (int i = 0; i &lt; str.length(); i++) {
          int val = str.charAt(i);
  
          if (char_set[val]) {
              return false;
          }
          char_set[val] = true;
      }
  
      return true;
  }
  </code></pre>
  
  <p>My question is, is my implementation slower than the one presented? I assume it is, but if a Hash look up is O(1) wouldn't they be the same complexity?</p>
  
  <p>Thank you.</p>