```
public class Solution {
public int lengthOfLongestSubstring(String s) {
if (s == null || s.length() == 0) {
return 0;
}
Set<Character> set = new HashSet<>();
int p1 = 0;
int p2 = 0;
int ret = 1;
while (p2 < s.length()) {
while (set.contains(s.charAt(p2))) {
set.remove(s.charAt(p1));
p1++;
}
set.add(s.charAt(p2));
ret = Math.max(ret, p2 - p1 + 1);
p2++;
}
return ret;
}
}
```