Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: Consider the string "xyzzyzyx". How many distinct substrings of length 2 are there?

  1. A. 5
  2. B. 6
  3. C. 4
  4. D. 7

Correct answer: A. 5

The length-2 substrings are xy, yz, zz, zy, yz, zy, yx, which contain five distinct values. Counting positions gives seven, which overcounts the repeats.

More Coding Interview Problems questions

Exams that ask this topic