#include <string>
#include <unordered_set>
 
using usetC = std::unordered_set<char>;
 
class Solution {
public:
    int lengthOfLongestSubstring(std::string s) {
        if (s.length() <= 1) return s.length();
        int left = 0, right = 0;
        
        usetC window;
        int maxLen = 0;
 
        while (right < s.length()) {
            // Grow the window till we are getting unique characters
            if (!window.contains(s[right]))
                window.insert(s[right++]);
            else {
                // Shrink the window till no duplicate characters remain
                maxLen = (window.size() > maxLen) ? window.size() : maxLen;
                window.erase(s[left++]);
            }            
        }
        
        // Final check since we were only updating maxLen when we had encountered duplicates
        maxLen = (window.size() > maxLen) ? window.size() : maxLen;
 
        return maxLen;
    }
};