Problem statement

https://binarysearch.com/problems/Longest-Distinct-Sublist/

Solution

Equal to Leetcode 0003. Longest Substring Without Repeating Characters.

Complexity

It is O(n) for time and space.

Code

class Solution:
    def solve(self, s):
        window = set()
        beg, end, ans, n = 0, 0, 0, len(s)
        
        while beg < n and end < n:
            if s[end] not in window:
                if end + 1 < n: window.add(s[end])
                end += 1
                ans = max(ans, end - beg)
            else:
                window.remove(s[beg])
                beg += 1
                
        return ans