title: Detecting if a string has unique characters: comparing my solution to "Cracking the Coding Interview?"
body: <p>I am working through the book "Cracking the Coding Interview" and I have come across questions here asking for answers, but I need help comparing my answer to the solution. My algorithm works, but I am having difficulty understand the solution in the book. Mainly because I don't understand what some of the operators are really doing. </p>
  
  <p>The task is: "Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures?"</p>
  
  <p>This is my solution:</p>
  
  <pre><code>public static boolean checkForUnique(String str){
      boolean containsUnique = false;
  
      for(char c : str.toCharArray()){
          if(str.indexOf(c) == str.lastIndexOf(c)){
              containsUnique = true;
          } else {
              containsUnique = false;
          }
      }
  
      return containsUnique;
  }
  </code></pre>
  
  <p>It works, but how efficient is this? I saw that the complexity of the index functions for String in Java are O(n*m)</p>
  
  <p>Here is the solution from the book:</p>
  
  <pre><code>public static boolean isUniqueChars(String str) {
      if (str.length() &gt; 256) {
          return false;
      }
      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>A couple things I am not quite understanding with the solution. First, what does the "|=" operator do? Why is 'a' subtracted from the current character in the string for the value of "val"? I know "&lt;&lt;" is a bitwise left shift, but what does <code>(checker &amp; (1&lt;&lt;val))</code> do? I know it is bitwise and, but I am not understanding it since I am not understanding the line where checker gets a value.</p>
  
  <p>I am just not familiar with these operations and unfortunately the book does not give an explanation of the solutions, probably because it assumes you already understand these operations.</p>