Data Structures & AlgorithmsModerateTCS NQT · string basics

Problem: How many distinct substrings of length 2 does 'abcabc' contain? Run the code.

Problem: How many distinct substrings of length 2 does 'abcabc' contain? Run the code.
s = 'abcabc'
print(len({s[i:i + 2] for i in range(len(s) - 1)}))
What is printed?
  1. A. 3
  2. B. 4
  3. C. 5
  4. D. 6

Correct answer: A. 3

The length-2 substrings are ab, bc, ca, ab, bc, so only 'ab', 'bc' and 'ca' are distinct.

More Coding Interview Problems questions

Exams that ask this topic