def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
if not s: return 0
rep = {s[0]:0}
l = len(s)
maxl = 1
i,j = 0,1
while j < l:
if s[j] in rep and rep[s[j]] >= i:
i = rep[s[j]] + 1
else:
maxl = max(maxl, ji+1)
rep[s[j]] = j
j += 1
return maxl
My Python solution with O(n) time and O(1) space

The in operator here is used to check if a key is in dict or not. The time complexity of this operation is O(1) in most cases.
Please take a look at this link for more details:
https://www.quora.com/WhatisthetimecomplexityofcheckingifakeyisinadictionaryinPython