Problem · String

Unique Digit Permutations Without Leading Zero

Learn this problem
MediumMicrosoft logoMicrosoftNEW GRADONSITE INTERVIEW
See Microsoft hiring insights

Problem 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) → long

Examples

Example 1

digits = "102"return = 4

The valid permutations are 102, 120, 201, and 210.

Example 2

digits = "100"return = 1

Only 100 is valid; permutations beginning with zero are excluded.

Example 3

digits = "000"return = 0

Every arrangement begins with zero.

Constraints

  • 1 <= digits.length() <= 20
  • digits contains only characters from 0 through 9.
  • The answer fits in a signed 64-bit integer.

More Microsoft problems

drafts saved locally
public long countUniquePermutations(String digits) {
  // write your code here
}
digits"102"
expected4
checking account