3. Longest Substring Without Repeating Characters
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int left = 0, right = 0, count = 0, len = 0;
unordered_map <char, int> map;
while(right< s.length()){
if(++map[s[right]] == 2) count++; // can also be ++map[s[right]] > 1
right++;
while(count == 1){ // can also be count > 0
if(--map[s[left]] == 1) count --; // can also be --map[s[left]] > 0
left++;
}
len = max(len, right - left);
}
return len;
}
};Last updated
Was this helpful?