AcWing——61. 最长不含重复字符的子字符串
双指针解法
class Solution {
public:
int longestSubstringWithoutDuplication(string s) {
unordered_map<char, int> hash; //hash['x'] = 1;
int ans = 0;
for(int i = 0, j = 0; j < (int)s.size(); ++j){
hash[s[j]]++; //hash['a']++; 0 1 2
while(hash[s[j]] > 1){ // dbfg
hash[s[i++]]--; //dbfg ij
}
ans = max(ans, j - i + 1);
}
return ans;
}
};