awelgorithms

Translate contiguous into a window

Updated

If the object is a contiguous slice, name it a[L..R] (or s[L..R]) and maintain L,R. Skipping a middle index is a subsequence, not a window.

What to focus on

The words contiguous, subarray, substring, or window all pick this object. After you name L and R, the contents are forced.

Worked examples

sum slice

a = [4, 1, 9, 1], contiguous sum ≤ 10
  1. [0,1] [4,1] is a window
  2. [0,2] includes 9

Result: window, not the pair 4 and 9

chars

s = "abca", contiguous unique
  1. s[0..2] "abc"
  2. s[0] with s[3] skipping b,c is not a window

Result: "abc" / "bca"

not contiguous

a = [4, 1, 9, 1], take 4 and 9
  1. indices 0 and 2, gap at 1

Result: subsequence, refuse as a window

Try it

Write isWindow(a, L, R). a is an array of integers, or s is a string. L is the left index. R is the right index. A window includes every index from L through R. A gap is a subsequence, not a window. Print window or not. Classify [4, 1], [4, 9], and [1, 9, 1] on [4, 1, 9, 1].

a (or s) is nonempty. 0 ≤ L ≤ R < n for any claimed window.