[Python] O(n) sliding window, explained
This is classical problem for sliding window. Let us keep window with elements [beg: end), where first element is included and last one is not. For example [0, 
This is classical problem for sliding window. Let us keep window with elements `[beg: end)`, where first element is included and last one is not. For example `[0, 0)` is empty window, and `[2, 4)` is window with `2` elements: `2` and `3`.\nLet us discuss our algorithm now:\n1. `window` is set of symbols in our window, we use set to check in `O(1)` if new symbol inside it or not.\n2. `beg = end = 0` in the beginning, so we start with empty window, also `ans = 0` and `n = len(s)`.\n3. Now, we continue, until one of two of our pointers reaches the end. First, we try to extend our window to the right: check `s[end] in window` and if we can, add it to set, move `end` pointer to the right and update `ans`. If we can not add new symbol to set, it means it is already in `window` set, and we need to move left pointer and move `beg` pointer to the right.\n\n**Complexity**: we move both of our pointers only to the left, so time complexity is `O(n)`. Space complexity is `O(1)`.\n\n```\nclass Solution:\n    def lengthOfLongestSubstring(self, s):\n        window = set()\n        beg, end, ans, n = 0, 0, 0, len(s)\n        \n        while beg < n and end < n:\n            if s[end] not in window:\n                if end + 1 < n: window.add(s[end])\n                end += 1\n                ans = max(ans, end - beg)\n            else:\n                window.remove(s[beg])\n                beg += 1\n                \n        return ans\n```\n\nIf you have any questions, feel free to ask. If you like solution and explanations, please **Upvote!**
['Two Pointers', 'Sliding Window']