Data Structures & AlgorithmsModerate

What is the length of the longest contiguous substring of "xxyxyyxyxxy" in which the number of 'x' letters is at most the number of 'y' letters?

  1. A. 9
  2. B. 21
  3. C. 10
  4. D. 11

Correct answer: C. 10

Map 'y' to +1 and 'x' to −1; the condition is a non-negative balance, which is answered with prefix sums and first occurrences of each prefix value. The strict version and the full string are different quantities.

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

Exams that ask this topic