class Solution { public int lengthOfLongestSubstring(String s) { int len = s.length(), max = 0; Map<Character, Integer> map = new HashMap<>(); //try to extend the range [start, end] int start = 0; for (int end = 0; end <len; end++) { if (map.containsKey(s.charAt(end))) { start = Math.max(map.get(s.charAt(end))+1,start); } max = Math.max(max, end - start + 1); map.put(s.charAt(end), end); } return max; } }