```
public class Solution {
public int lengthOfLongestSubstring(String s) {
int max = 0;
int left = 0, right = 0;
int len = s.length();
if(len == 0 || len == 1) return len;
Map<Character, Integer> map = new HashMap<>();
map.put(s.charAt(0), 0);
for(int i=1; i<len; i++) {
char c = s.charAt(i);
right = i;
if(map.containsKey(c)) {
int index = map.get(c);
if(index >= left) {
left = index+1;
}
}
map.put(c, i);
max = Math.max(max, right-left+1);
}
return max;
}
}
```