public class Solution { public int lengthOfLongestSubstring(String s) { int max = 0; if (s == null || s.length() == 0) { return 0; } HashSet<Character> hs = new HashSet<Character>(); int left = 0; for (int i = 0; i < s.length(); i++) { if (!hs.contains(s.charAt(i))) { hs.add(s.charAt(i)); max = Math.max(max, i - left + 1); } else { while (left < i && s.charAt(left) != s.charAt(i)) { hs.remove(s.charAt(left)); left++; } left++; } } return max; } }