/*
1:cMap[s[I]] 存储当前字段出现的最大 index,初始为 -1
2:pre 为最近的重复字段的 index
3:i - pre 为当前字段与前面字段中出现重复的字段的间隔字符数
*/
class Solution {
public:
int lengthOfLongestSubstring(string s) {
if (s.length() <= 0) return 0;
map <char, int>cMap;
int mmax = 0;
int pre = -1;
for (int i = 0; i < s.length(); i++) cMap[s[i]] = -1;
for (int i = 0; i < s.length(); i++) {
pre = max(pre, cMap[s[i]]);
mmax = max(mmax, i - pre);
cMap[s[i]] = i;
}
return mmax;
}
};