Data Structures & AlgorithmsHardGATE CS · Infosys

How many distinct non-empty substrings does the string ababa have?

  1. A. 15
  2. B. 12
  3. C. 9
  4. D. 5

Correct answer: C. 9

Of the 15 substrings, a appears 3 times and b, ab, ba, aba twice each, so 15 − 6 = 9 are distinct. Removing only repeated single letters gives 12, 15 ignores repeats entirely, and 5 counts the distinct palindromic substrings.

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

Exams that ask this topic