Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: Count the distinct non-empty substrings of "abcabc" (two substrings are the same if their letters are identical, regardless of position).

  1. A. 21
  2. B. 16
  3. C. 15
  4. D. 12

Correct answer: C. 15

A set of all substrings keeps 3 + 3 + 3 + 3 + 2 + 1 = 15 distinct strings by length. Counting index ranges gives 6·7/2 = 21, adding the empty string gives 16, and dropping the single letters gives 12.

More Coding Interview Problems questions

Exams that ask this topic