Longest Substring Without Repeating Characters
3. Longest Substring Without Repeating Characters
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
hash_dict = {}
ans = 0
i = 0
for j, w in enumerate(s):
i = max(i, hash_dict.get(w, -1) + 1)
ans = max(ans, j - i + 1)
hash_dict[w] = j
return ans
Reference: