Data Structures & AlgorithmsHardICPC regional · combinatorics

Problem: How many non-decreasing strings of length 3 over the digits {0, 1, 2} exist? Run the code.

Problem: How many non-decreasing strings of length 3 over the digits {0, 1, 2} exist? Run the code.
from itertools import product
print(sum(1 for t in product(range(3), repeat=3) if all(t[i] <= t[i + 1] for i in range(2))))
What is printed?
  1. A. 9
  2. B. 12
  3. C. 10
  4. D. 27

Correct answer: C. 10

The count is C(3 + 3 - 1, 3) = C(5, 3) = 10 by stars and bars.

More Coding Interview Problems questions

Exams that ask this topic