From a character string of length m, the number of sub-strings of all lengths…

2023

From a character string of length m, the number of sub-strings of all lengths that can be formed are:

  1. A.

    m2

     

  2. B.

    m

  3. C.

    m(m+1)/2

  4. D.

    m(logm)

Attempted by 228 students.

Show answer & explanation

Correct answer: C

Concept: A substring of a string is any contiguous run of characters within it, and each occurrence of a substring is uniquely fixed by choosing a start position and an end position with start ≤ end (this counts substrings by position/occurrence, not distinct string values — repeated characters do not merge positions). So counting “all substrings of all lengths” is the same as counting how many valid (start, end) position pairs exist — which reduces to summing consecutive integers, the same triangular-number pattern used to count pairings among a set of positions.

  1. Number the character positions of the string 1 through m.

  2. A substring starting at position i can end at position i, i+1, …, or m — that gives (m − i + 1) valid substrings starting at position i.

  3. Summing this count over every starting position i = 1, 2, …, m gives: m + (m−1) + (m−2) + … + 1.

  4. This is the sum of the first m positive integers — an arithmetic series whose value is m(m+1)/2.

Cross-check: for m = 3 (string “abc”), the substrings are a, b, c (length 1), ab, bc (length 2), and abc (length 3) — 3 + 2 + 1 = 6 in total, and the formula gives 3(3+1)/2 = 6. The two match, confirming m(m+1)/2.

Explore the full course: Niacl Ao It Specialist