MemotivaLeetCode Patterns Flashcards: Sliding Window, Fixed Size, Dynamic Size, Substring

How do you find the longest substring without repeating characters?

LeetCode Patterns Flashcards: Sliding Window, Fixed Size, Dynamic Size, Substring

Аудио-карточка · 0:30

Nortren·

How do you find the longest substring without repeating characters?

0:30

Use a dynamic sliding window with a hash set tracking characters in the current window. Expand the right pointer to add characters. When a character already exists in the set, shrink from the left, removing characters from the set, until the duplicate is gone. At each step, update the maximum length as right minus left plus one. A hash map storing the last index of each character offers a small optimization: when a duplicate is found, jump the left pointer directly past the previous occurrence instead of shrinking one character at a time. Both approaches run in O of n time.
neetcode.io