title: Determining a string has all unique characters without using additional data structures and without the lowercase characters assumption
body: <p>This is one of the questions in the <a href="http://www.careercup.com/book" rel="nofollow noreferrer"><em>Cracking the Coding Interview</em> book</a> by Gayle Laakmann McDowell:</p>
  
  <blockquote>
    <p>Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?</p>
  </blockquote>
  
  <p>The author wrote:</p>
  
  <blockquote>
    <p>We can reduce our space usage a little bit by using a bit vector. We will assume, in the below code, that the string is only lower case <code>'a'</code> through <code>'z'</code>. This will allow us to use just a single int.</p>
  </blockquote>
  
  <p>The author has this implementation:</p>
  
  <pre><code>public static boolean isUniqueChars(String str) {
      int checker = 0;
      for (int i = 0; i &lt; str.length(); ++i) {
          int val = str.charAt(i) - 'a';
          if ((checker &amp; (1 &lt;&lt; val)) &gt; 0)
              return false;
          checker |= (1 &lt;&lt; val);
      }
      return true;
  }
  </code></pre>
  
  <p>Let's say we get rid of the assumption that "the string is only lower case <code>'a'</code> through <code>'z'</code>". Instead, the string can contain any kind of character&mdash;like ASCII characters or Unicode characters.</p>
  
  <p>Is there a solution as efficient as the author's (or a solution that comes close to being as efficient as the author's)?</p>
  
  <p>Related questions:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/19484406/detecting-if-a-string-has-unique-characters-comparing-my-solution-to-cracking">Detecting if a string has unique characters: comparing my solution to &quot;Cracking the Coding Interview?&quot;</a></li>
  <li><a href="https://stackoverflow.com/questions/9141830/explain-the-use-of-a-bit-vector-for-determining-if-all-characters-are-unique">Explain the use of a bit vector for determining if all characters are unique</a></li>
  <li><a href="https://stackoverflow.com/questions/2589610/string-unique-characters">String unique characters</a></li>
  <li><a href="https://stackoverflow.com/questions/17357370/implementing-an-algorithm-to-determine-if-a-string-has-all-unique-characters">Implementing an algorithm to determine if a string has all unique characters</a></li>
  <li><a href="https://stackoverflow.com/questions/4987863/determine-if-a-string-has-all-unique-characters">determine if a string has all unique characters?</a></li>
  </ul>