awelgorithms

Each index enters the window once

Updated

R starts left of the array and only increases. Index i becomes R at the moment it enters, and R never goes back to i. That is one include of a[i] (or s[i]) in the whole scan.

What to focus on

Do not reset R to 0 for a new L. That would re-enter indices and blow past O(n). Nested for-L-for-R is not this pointer.

Worked examples

four enters

a = [4, 1, 9, 1]
  1. R=0 enter 4
  2. R=1 enter 1
  3. R=2 enter 9
  4. R=3 enter 1

Result: 4 includes, one per index

string

s = "abca"
  1. R visits 0,1,2,3 once each
  2. the second a enters only as index 3

Result: n enters, not n²

unique abcabcbb

s = "abcabcbb"
  1. R=0..7 each once
  2. shrinks move L, never rewind R

Result: 8 enters

Try it

Write logEnter(a). a is an array of integers, or s is a string. R is the right index and only increases. Index i enters the window once, when it becomes R. Print enter(i) for i = 0, 1, …, n-1 with no repeats. Return nothing. On [4, 1, 9, 1], "abca", and "abcabcbb".

a (or s) is nonempty. R only increases.