Problem · String

Permutations of Unique Characters

Learn this problem
MediumSalesforce logoSalesforceFULLTIMEPHONE SCREEN
See Salesforce hiring insights

Problem statement

Given a non-empty string word whose characters are all distinct, return every permutation of its characters.

Return the permutations in lexicographic order.

Function

generatePermutations(word: String) → String[]

Examples

Example 1

word = "abc"return = ["abc","acb","bac","bca","cab","cba"]

There are 3! = 6 permutations, listed lexicographically.

Example 2

word = "z"return = ["z"]

A one-character word has exactly one permutation.

Constraints

  • 1 <= word.length <= 9
  • word contains distinct lowercase English letters.

More Salesforce problems

drafts saved locally
public String[] generatePermutations(String word) {
    // Write your code here.
}
word"abc"
expected["abc", "acb", "bac", "bca", "cab", "cba"]
checking account