privatestaticclassSolution{ publicintlengthOfLongestSubstring(String s){ if (s.length()==0) { return0; } HashMap<Character, Integer> cache = new HashMap<>(); int max = 0; int left = 0;
for (int i = 0; i < s.length(); i++) { if (cache.containsKey(s.charAt(i))){ left = Math.max(left, cache.get(s.charAt(i)) + 1); }
max = Math.max(max, i - left + 1); cache.put(s.charAt(i), i);