Data Structures & AlgorithmsHardCodeforces Div2 C · GATE CS

Sort all suffixes of "banana" lexicographically. What is the sum of the lengths of the longest common prefixes of each pair of adjacent suffixes in that order?

  1. A. 3
  2. B. 7
  3. C. 15
  4. D. 6

Correct answer: D. 6

Sorted suffixes: a, ana, anana, banana, na, nana, with adjacent LCPs 1, 3, 0, 0, 2 = 6. Summing LCPs over all pairs gives 7, 3 is only the largest LCP ('ana'), and 15 is the number of distinct substrings.

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

Exams that ask this topic