How many substrings of different lengths (non-zero) can be found formed from a character string of length $n$?
How many substrings of different lengths (non-zero) can be found formed from a character string of length $n$?
(A) $n$,
(B) $n^2$,
(C) $\frac{n(n - 1)}{2}$,
(D) $\frac{n(n + 1)}{2}$.
(GATE 1994)
Answer: (D) $\frac{n(n + 1)}{2}$.
Explanation:
The given string has length $n$.
No. of substrings of length $n = 1$
No. of substrings of length $n - 1 = 2$
$\cdots \cdots \cdots \cdots \cdots \cdots$
$\cdots \cdots \cdots \cdots \cdots \cdots$
No. of substrings of length $1 = n$
Therefore, the total number of substrings
$= 1 + 2 + \cdots + n$
$= \frac{n(n + 1)}{2}$.
Comments
Post a Comment