FastPrepString Permutations in Custom Character Order
Problem · Backtracking

String Permutations in Custom Character Order

Learn this problem
MediumWex logoWexFULLTIMEPHONE SCREEN

Problem statement

Given a string s containing digits, lowercase English letters, and uppercase English letters, return every distinct permutation of its characters in the custom lexicographic order defined below.

  • Digits come first, in order 0 through 9.
  • Lowercase letters come next, in order a through z.
  • Uppercase letters come last, in order A through Z.

Compare two permutations at their first different character using this order. Each permutation must use every character of s exactly as many times as it appears in s.

For this exercise, assume identical permutations caused by repeated characters appear only once. The empty string has exactly one permutation: the empty string.

Function

sortedPermutations(s: String) → String[]

Examples

Example 1

s = "0aA"return = ["0aA","0Aa","a0A","aA0","A0a","Aa0"]

Permutations beginning with 0 come first, followed by those beginning with a, then A. Within each group, the same custom order applies to the remaining characters.

Example 2

s = "b1b"return = ["1bb","b1b","bb1"]

The two identical b characters produce only three distinct permutations. The digit 1 comes before b.

Example 3

s = ""return = [""]

Using every character of an empty string produces the empty string once.

Constraints

  • 0 <= s.length <= 7.
  • Each character is an English letter or a digit.

More Wex problems

drafts saved locally
public String[] sortedPermutations(String s) {
    // Write your code here.
}
s"0aA"
expected["0aA", "0Aa", "a0A", "aA0", "A0a", "Aa0"]
checking account