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