```
public int lengthOfLongestSubstring(String s) {
if(s == null || s.length() == 0) return 0;
Map<Character, Integer> map = new HashMap<>();
int res = 0, curr = 0;
for(int i = 0; i < s.length(); i++){
if(!map.containsKey(s.charAt(i)) || i - map.get(s.charAt(i)) > curr){
curr++;
}else{
res = Math.max(curr, res);
curr = Math.min(curr, i - map.get(s.charAt(i)));
}
map.put(s.charAt(i),i);
}
res = Math.max(curr, res);
return res;
}
```