classSolution{ publicintlengthOfLongestSubstring(String s){ int n = s.length(); Set<Character> eh = new HashSet<>(); int ans = 0, i = 0, j = 0; while (i < n && j < n) { if (!eh.contains(s.charAt(j))) { eh.add(s.charAt(j++)); ans = Math.max(ans, j - i); } else { eh.remove(s.charAt(i++)); } } return ans; } }