import java.util.*;
public class Solution {
public int lengthOfLongestSubstring (String s) {
//创建一个hash表存放字母和值
HashMap<Character, Integer> map = new HashMap<>();
int max = Integer.MIN_VALUE;
//两个指针从头开始
//右指针先走
for (int left = 0, right = 0; right < s.length(); right++) {
//判断map中是否已经含有key
if (map.containsKey(s.charAt(right)))
map.put(s.charAt(right), map.get(s.charAt(right)) + 1);
else
map.put(s.charAt(right), 1);
//当right大于1的时候,说明有重复
while (map.get(s.charAt(right)) > 1)
//把左指针右移,直到没有重复为止(括号中left++,是执行完后+1)
map.put(s.charAt(left), map.get(s.charAt(left++)) - 1);
max = Math.max(max, right - left + 1);
}
return max;
}
}