Longest Substring Without Repeat

int Solution::lengthOfLongestSubstring(string A) {
    unordered_set<char> s;
    int begin = -1;
    int end = -1;
    int maxLength = 0;
    for(const char &ch: A) {
        while(s.find(ch) != s.end()) {
            begin++;
            s.erase(A[begin]);
        } 
        
        end++;
        s.insert(A[end]);
        maxLength = max(maxLength, end - begin);
    }
    
    return maxLength;
}

Accepted

Time Complexity: O(n)O(n)

Space Complexity: O(n)O(n)

Last updated