public int lengthOfLongestSubstring(String s) {

    if (s == null || s.length() == 0) {
      return 0;
    }

    Set<Character> set = new HashSet<Character>();
    int maxLen = 1;
    int head = 0;
    int tail = 0;
    set.add(s.charAt(0));

    // this is also a popular for loop style
    // just like a while loop
    for (tail = 1 ; tail < s.length() ;) {

      char ch = s.charAt(tail);
      if (!set.contains(ch)) {
        set.add(ch);
        tail++;

        int temp = tail - head;
        if (temp > maxLen) {
          maxLen = temp;
        }

      } else {
        while (s.charAt(head) != ch) {
          set.remove(s.charAt(head));
          head++;
        }
        set.remove(s.charAt(head));
        head++;
      }
    }

    return maxLen;
  }