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]- R=0 enter 4
- R=1 enter 1
- R=2 enter 9
- R=3 enter 1
Result: 4 includes, one per index
string
s = "abca"- R visits 0,1,2,3 once each
- the second a enters only as index 3
Result: n enters, not n²
unique abcabcbb
s = "abcabcbb"- R=0..7 each once
- 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.