Sign inEnroll free
DSA Visualizer · 25 visualizationsLongest Substring Without Repeating Characters
MediumSliding Window

Longest Substring Without Repeating Characters

Given a string s, find the length of the longest substring without repeating characters.

The step-through player is coming for this one. Two Sum has the full animation today; the rest are being authored in the admin.
Pythontime O(n) · space O(1)
def lengthOfLongestSubstring(s: str) -> int:
    seen = {}
    l = best = 0
    for r, c in enumerate(s):
        if c in seen and seen[c] >= l:
            l = seen[c] + 1
        seen[c] = r
        best = max(best, r - l + 1)
    return best
Two Sum is free. Enroll to unlock every visualization and save where you stopped.Enroll free
Watch the video walkthrough