Data Structures & AlgorithmsModerateProduct Company SDE · CP practice

Problem: How many three-letter words over {a, b, c} contain every letter at least once? Run the code.

Problem: How many three-letter words over {a, b, c} contain every letter at least once? Run the code.
from itertools import product
print(sum(1 for t in product('abc', repeat=3) if len(set(t)) == 3))
What is printed?
  1. A. 27
  2. B. 24
  3. C. 6
  4. D. 3

Correct answer: C. 6

A length-3 word using all of a, b, c is a permutation: 3! = 6. 27 counts all words, 24 counts words with at least two distinct letters, and 3 counts the letters.

More Coding Interview Problems questions

Exams that ask this topic