Data Structures & AlgorithmsModerateGATE CS · DSA

How many distinct non-empty substrings does the string "aaaaa" have, and how many substrings counted by position?

  1. A. 5 and 5
  2. B. 5 and 15
  3. C. 15 and 15
  4. D. 1 and 15

Correct answer: B. 5 and 15

By position there are n(n + 1)/2 = 15 substrings, but they are just a, aa, aaa, aaaa, aaaaa — 5 distinct strings.

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

Exams that ask this topic