Python solution
We initialize the result res = 0, and two pointers j = 0, and i = 0. We initialize a dictionary dic which maps every element in s[:i+1] to its index of rightmos
We initialize the result `res = 0`, and two pointers `j = 0`, and `i = 0`. We initialize a dictionary `dic` which maps every element in `s[:i+1]` to its index of rightmost occurrence in `s[:i+1]`. Then we iterate `i` over `range(len(s))`, if `s[i]` is not in `dic`, we add it to `dic`: `dic[s[i]] = i`; Otherwise, we move `j` to `max(j, dic[s[i]]+1)`, so that within the window `[j:i+1]`, the element `s[i]` only occur once. Then we update the result `res = max(res, i-j+1)`. Finally, we return `res`.\n\nTime complexity: `O(n)`, space complexity: `O(n)`.\n\n```\nclass Solution(object):\n    def lengthOfLongestSubstring(self, s):\n        """\n        :type s: str\n        :rtype: int\n        """\n        dic = {}\n        res = 0\n        j = 0\n        for i in range(len(s)):\n            if s[i] not in dic:\n                dic[s[i]] = i\n            else:\n                j = max(j, dic[s[i]]+1)\n                dic[s[i]] = i\n            res = max(res, i-j+1)\n        return res\n```
[]