Skip to content
DeepForgeby svx
All problems
MediumAlgorithmsal-151

Count Distinct Permutations

Problem

Given nums that may contain duplicates, return the number of distinct permutations of all elements.

The answer is the multinomial coefficient n! divided by the factorial of every value's frequency. An empty list has exactly one permutation.

Hint

Dividing by the repeated frequencies removes permutations that only swap equal values.

Test cases

case 1
in: [[1,2,3]]
expected: 6
case 2
in: [[1,1,2]]
expected: 3
case 3
in: [[1,1,1]]
expected: 1
case 4
in: [[]]
expected: 1
case 5
in: [[1,2,2,3]]
expected: 12
Starter codePython
def count_distinct_permutations(nums):
    # Your code here
    pass

5 test cases · run your code against them in the editor