GATE / CS ARCHIVE

GATE CSE 1994 / QUESTION 1.15

Counting substring occurrences

Theory of Computation · Other / Mixed Topics · Full GATE CSE 1994 paper

GATE 1994, Q1.15 MCQ
Report issue

Other / Mixed Topics

Question 1.15, GATE 1994: For a character string of length n, how many nonempty contiguous substrings are there when counted by position?
Tap to enlarge
Your answer
Answer & solution

Answer: D: n(n+1)/2 (no official key was published)

A substring of length k can start in n−k+1 positions. Summing for k=1 through n gives n+(n−1)+…+1=n(n+1)/2. Equivalently choose its two boundaries among n+1 boundary positions. This counts occurrences, not necessarily distinct string values: repeated characters can make different positions yield the same word. It also excludes ε. If “substrings of all lengths” includes the empty word or means distinct values, the answer needs that additional convention.

Original paper, page 2