Data Structures & AlgorithmsVery hardCodeforces Div2 D · TCS NQT

What is the length of the shortest contiguous substring of "xaybzcabbc" that contains "abc" as a SUBSEQUENCE (the letters appear in order, not necessarily next to each other)?

  1. A. 3
  2. B. 6
  3. C. 4
  4. D. 5

Correct answer: C. 4

For each start, greedily match the pattern letters in order and stop as soon as the last letter is matched; the minimum over starts is the answer. Requiring the letters only to appear in any order (a set check) finds a shorter window that does not respect the order.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic