public class LongestSubstring { public static int lengthOfLongestSubstring(String s) { int length = s.length(); if(length<2) {return length;} Set set = new HashSet(); int res = 0; int start = 0; int end = 0; while (end<length){ if(!set.contains(s.charAt(end))){ set.add(s.charAt(end++)); res = Math.max(res,set.size()); }else{ set.remove(s.charAt(start++)); } }
return res; }
public static void main(String[] args) { int result = lengthOfLongestSubstring("abcabcbb"); System.out.println(result); } }