class Solution {
public:
int lengthOfLongestSubstring(string s) {
unordered_map<char, int> pre;
int left, right;
left = right = 0;
int maxlen = 0;
for(int i=0;i<s.size();++i){
char ch = s[right];
auto preIt = pre.find(ch);
//如果没找到或者在left之前
if(preIt == pre.end() || preIt->second < left){
pre[ch] = right;
right++;
if(right == s.size()){
int nowlen = right - left;
maxlen = max(maxlen, nowlen);
}
}else{
//left到right之间有重复
int nowlen = right - left;
maxlen = max(maxlen, nowlen);
left = pre[ch]+1;
pre[ch] = right;
right++;
}
}
return maxlen;
}
};**