```
public static int lengthOfLongestSubstring(String s) {
Set<Character> set = new HashSet() ;
int i = 0 ;
int j = 0 ;
int maxLen = 0 ;
char[] sArray = s.toCharArray() ;
int len = s.length() ;
while (j < len){
if (set.contains(sArray[j])){
maxLen = Math.max(maxLen,j-i) ;
while(sArray[i] != sArray[j]){
set.remove(sArray[i]) ;
i++ ;
}
i++ ;
j++ ;
}else{
set.add(sArray[j]) ;
j++;
}
}
maxLen = Math.max(maxLen ,len -i) ;
return maxLen ;
}
```