```
public static int lengthOfLongestSubstring2(String s) {
if (s.length() == 0)
return 0;
int longest = 1;
String sub = "" + s.charAt(0);
for (int end = 1; end < s.length(); end++) {
int index = sub.indexOf(s.charAt(end));
if (index != -1) {
longest = (sub.length() > longest) ? sub.length() : longest;
sub = sub.substring(index + 1, sub.length());
}
sub += s.charAt(end);
}
longest = (sub.length() > longest) ? sub.length() : longest;
return longest;
}
```