classSolution{publicintlengthOfLongestSubstring(String s){HashSet<Character> hs =newHashSet<>();int res =0;int right =0;for(int i =0; i < s.length();i++){// 移除前一个if(i !=0){
hs.remove(s.charAt(i -1));}while(right < s.length()&&!hs.contains(s.charAt(right))){
hs.add(s.charAt(right));
right++;}
res =Math.max(res, hs.size());}return res;}}
3- ACM 实现
publicclass noDuplicate {privatestaticintnonDuplicate(String s){HashSet<Character> hs =newHashSet<>();int res =0;int right =0;// 2.遍历for(int i =0; i < s.length();i++){// 2.2 去重if(i>0){
hs.remove(s.charAt(i-1));}// 2.3 whilewhile(right<s.length()&&!hs.contains(s.charAt(right))){
hs.add(s.charAt(right));
right++;}
res =Math.max(res,hs.size());}return res;}publicstaticvoidmain(String[] args){Scanner sc =newScanner(System.in);System.out.println("输入字符串");String s = sc.nextLine();System.out.println("结果是"+nonDuplicate(s));}}