awelgorithms

Window on characters not words

Updated

s[i] is one character. L and R are character indices. The window is s[L]+s[L+1]+…+s[R], including every in-between character. Spaces, if present, are cells too.

What to focus on

Do not split on whitespace or tokens unless the problem says words. "abc" is three cells. ADOBECODEBANC is 13 cells, not three words.

Worked examples

four cells

s = "abca", L=0 R=2
  1. indices 0,1,2
  2. chars a,b,c
  3. not the word abca as one unit

Result: "abc"

unique cells

s = "abcabcbb", L=0 R=2
  1. s[0]='a', s[1]='b', s[2]='c'
  2. length 3 characters

Result: "abc"

cover cells

s = "ADOBECODEBANC", t = "ABC", L=9 R=12
  1. s[9..12] B,A,N,C
  2. N is a character in the window

Result: "BANC"

Try it

Write charsIn(s, L, R). s is a string. L is the left character index. R is the right character index. The window includes every character from L through R. Length is R-L+1 characters, not words. Print every s[i] in the window. Return nothing. In "abca", in "abcabcbb"[0..2], and in ADOBECODEBANC[9..12].

s is a nonempty string. 0 ≤ L ≤ R < n. t is nonempty when covering.