Problem · String

Generate All String Permutations

Learn this problem
MediumMicrosoft logoMicrosoftINTERNONSITE INTERVIEW
See Microsoft hiring insights

Problem statement

Given a string s whose characters are distinct lowercase English letters, return every permutation of its characters.

Return the permutations in lexicographic order.

Function

generatePermutations(s: String) → String[]

Examples

Example 1

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

All six arrangements of the three distinct characters are returned in lexicographic order.

Example 2

s = "ba"return = ["ab","ba"]

The input order does not determine the output order; the two permutations are sorted lexicographically.

Example 3

s = "x"return = ["x"]

A one-character string has exactly one permutation.

Constraints

  • 1 <= s.length <= 8
  • s contains distinct lowercase English letters.

More Microsoft problems

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