测试用例:
{"abcabcbb",3}{"bbbbb",1}{"pwwkew",4} {"dvdf",3}
算法如下
public int lenghOfLongestSubString(String src) {
Map<Character, Integer> map = new HashMap<>();
int left=0;//子字符串的开始位置
int max=0;//子字符串的最大长度
char[] srcArray = src.toCharArray();
for (int i=0;i<srcArray.length;i++) {
int j;
if (map.containsKey(srcArray[i])) {
j = map.get(srcArray[i]);
if (left <= j) {
left = j + 1;
}
}
map.put(srcArray[i], i);
max = Math.max(i - left + 1, max);
}
return max;
}