Data Structures & AlgorithmsModerateGATE CS · ICPC regional

What is the length of the longest substring of aabbbcca in which every letter occurs at most twice?

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

Correct answer: A. 5

A sliding window with letter counts, shrinking whenever a letter exceeds two occurrences, finds the longest valid window in linear time.

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

Exams that ask this topic