DSA Visualizer · 25 visualizationsLongest Substring Without Repeating Characters
Arrays & Hashing
Two SumValid AnagramContains DuplicateGroup AnagramsTop K Frequent ElementsProduct of Array Except SelfLongest Consecutive SequenceStack
Valid ParenthesesBinary Search
Binary SearchTrees & BFS
Binary Tree Level OrderGraphs
Number of IslandsTrees
Invert Binary TreeMediumSliding 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 bestTwo Sum is free. Enroll to unlock every visualization and save where you stopped.Enroll free
Watch the video walkthrough