Data Structures & AlgorithmsModerateTCS NQT · counting

Problem: How many distinct permutations (rearrangements as strings) of 'aabc' exist? Run the code.

Problem: How many distinct permutations (rearrangements as strings) of 'aabc' exist? Run the code.
from itertools import permutations
print(len(set(permutations('aabc'))))
What is printed?
  1. A. 24
  2. B. 6
  3. C. 4
  4. D. 12

Correct answer: D. 12

The multinomial formula gives 4! / 2! = 12 distinct strings. Counting all 24 tuples from permutations overcounts because the two a's are identical.

More Coding Interview Problems questions

Exams that ask this topic