Skip to content

GATE CS 1998 Q1.23 — Combinatorics: Counting, Recurrence Relations, Generating Functions

Explain with AI

How many sub strings of different lengths (non-zero) can be formed from a character string of length nn?

Choose one option
View worked solution

Answer: D

To find the number of non-empty (non-zero length) substrings that can be formed from a character string of length nn (assuming all characters in the string are distinct):

We can classify the substrings by their length kk, where 1≤k≤n1 \le k \le n:

  • Substrings of length 11: There are nn such substrings.
  • Substrings of length 22: There are n−1n - 1 such substrings.
  • Substrings of length 33: There are n−2n - 2 such substrings.
  • …\dots
  • Substrings of length nn: There is 11 such substring.

Summing these up, the total number of non-empty substrings is:

Total Substrings=n+(n−1)+(n−2)+⋯+1=n(n+1)2\text{Total Substrings} = n + (n - 1) + (n - 2) + \dots + 1 = \frac{n(n + 1)}{2}

Alternative Approach:

A substring is uniquely defined by choosing its starting position ii and ending position jj such that 1≤i≤j≤n1 \le i \le j \le n.

  • The number of ways to choose two distinct indices i<ji < j is (n2)\binom{n}{2}.
  • The number of ways to choose i=ji = j (substrings of length 1) is nn.

Total substrings = (n2)+n=n(n−1)2+n=n(n+1)2\binom{n}{2} + n = \frac{n(n-1)}{2} + n = \frac{n(n+1)}{2}.

Thus, the correct option is D.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Combinatorics: Counting, Recurrence Relations, Generating Functions questions