Problem · String
Unique Digit Permutations Without Leading Zero
Learn this problemProblem statement
Given a nonempty decimal string digits, count the distinct permutations that use every occurrence exactly once and do not begin with 0.
Equal digits are indistinguishable. Return 0 when no valid permutation exists.
Function
countUniquePermutations(digits: String) → longExamples
Example 1
digits = "102"return = 4The valid permutations are 102, 120, 201, and 210.
Example 2
digits = "100"return = 1Only 100 is valid; permutations beginning with zero are excluded.
Example 3
digits = "000"return = 0Every arrangement begins with zero.
Constraints
1 <= digits.length() <= 20digitscontains only characters from0through9.- The answer fits in a signed 64-bit integer.