3. Longest Substring Without Repeating Characters
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 37% Topics: Hash Table, String, Sliding Window
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(1)
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
result, left = 0, 0
lookup = {}
for right in xrange(len(s)):
if s[right] in lookup:
left = max(left, lookup[s[right]]+1)
lookup[s[right]] = right
result = max(result, right-left+1)
return result
Solution from kamyu104/LeetCode-Solutions · MIT