GATE / CS ARCHIVE

GATE CSE 1998 / QUESTION 1.23

Counting substring occurrences

Algorithms · Other / Mixed Topics · Full GATE CSE 1998 paper

GATE 1998, Q1.23 1 mark · MCQ
Report issue

Other / Mixed Topics

Question 1.23, GATE 1998: How many nonempty substrings of all lengths can be formed from a length-n string? Choices include n, n², 2^n, n (n+1)/2.
Tap to enlarge
Your answer
Answer & solution

Answer: D for positional occurrences (no official key was published)

A substring is determined by its starting and ending positions. For length k there are n−k+1 placements. Summing these counts from k=1 to n gives n+(n−1)+…+1=n (n+1)/2, D. This counts occurrences, even when their text is identical. If the phrase “different lengths” means counting length values alone, there are only n; if it means distinct substring contents, the answer depends on the string. The standard occurrence interpretation is explicitly used.

Original paper, page 2