//0049最长不含重复字符的子字符串
class Solution {
public:
int longestSubstringWithoutDuplication(string s) {
vector<int>count(26, 0);
int ans = 0, len = s.length();
int l = 0, r = 0;
while(l <= r && r < len) {
count[s[r] - 'a']++;
//保证字符串合法
if (count[s[r] - 'a'] > 1) {
while (count[s[r] - 'a'] > 1) {
count[s[l] - 'a']--;
++l;
}
}
//之前的代码在if后面的else里面加的r++,有的情况不会成立
r++;
ans = max(ans, r - l);
}
return ans;
}
};