class Solution { public int lengthOfLongestSubstring(String s) { // 不包含重复字符的 if (s == null || s.length() == 0) return 0; char[] chars = s.toCharArray(); Map<Character,Integer> windows = new HashMap<>(); int left = 0, right = 0, maxLen = 0, len = s.length(); while (right < len) { char add = chars[right++]; // 放入map中 windows.put(add, windows.getOrDefault(add, 0) + 1); while (windows.get(add) > 1) { char remove = chars[left++]; // update windows.put(remove, windows.get(remove) - 1); } // get maxLen maxLen = Math.max(maxLen, right - left); } return maxLen; }}