publicclassSolution { publicintlengthOfLongestSubstring(String s) { intn= s.length(), ans = 0; Map<Character, Integer> map = newHashMap<>(); // current index of character // try to extend the range [i, j] for (intj=0, i = 0; j < n; j++) { if (map.containsKey(s.charAt(j))) { i = Math.max(map.get(s.charAt(j)), i); } ans = Math.max(ans, j - i + 1); map.put(s.charAt(j), j + 1); } return ans; } }
publicclassSolution { publicintlengthOfLongestSubstring(String s) { intn= s.length(), ans = 0; int[] index = newint[128]; // current index of character // try to extend the range [i, j] for (intj=0, i = 0; j < n; j++) { i = Math.max(index[s.charAt(j)], i); ans = Math.max(ans, j - i + 1); index[s.charAt(j)] = j + 1; } return ans; } }