1. Longest substring without repeats
medium25 XPReturn the length of the longest substring of s with no repeated characters. O(n) please.
Ctrl/⌘ + Enter runs the tests · Esc then Tab leaves the editor
Module 3 · Lesson 2 of 2
Three more patterns that cover a large share of coding screens. ~40 min
Sliding window: for "longest/shortest substring/subarray such that…" keep a window [left, right], expand right, and shrink left while the window is invalid.
Stack: for matching brackets, undo, "next greater element". Python lists are stacks: append() and pop() are O(1).
def bisect_left(xs, target):
lo, hi = 0, len(xs)
while lo < hi:
mid = (lo + hi) // 2
if xs[mid] < target:
lo = mid + 1
else:
hi = mid
return loReturn the length of the longest substring of s with no repeated characters. O(n) please.
Ctrl/⌘ + Enter runs the tests · Esc then Tab leaves the editor
Write is_valid(s) returning True if every (, [, { is closed by the matching bracket in the right order. Other characters are ignored.
Ctrl/⌘ + Enter runs the tests · Esc then Tab leaves the editor
A sorted list of unique ints was rotated (e.g. [4,5,6,7,0,1,2]). Return the index of target or -1, in O(log n).
Ctrl/⌘ + Enter runs the tests · Esc then Tab leaves the editor