FastPrepPermutations II

Permutations II

LinkedIn logoLinkedIn● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Given a lowercase string s, return every distinct permutation of its characters exactly once.

Return the permutations in ascending lexicographic order. Equal characters from different input positions must not create duplicate strings.

Function

uniquePermutations(s: String) → String[]

Examples

Example 1

s = "aab"return = ["aab","aba","baa"]

There are three distinct arrangements after duplicate swaps of the two a characters are removed.

Example 2

s = "xxyy"return = ["xxyy","xyxy","xyyx","yxxy","yxyx","yyxx"]

The four positions have six unique arrangements because each character appears twice.

Example 3

s = "z"return = ["z"]

A one-character string has one permutation.

Constraints

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

More LinkedIn problems

See LinkedIn hiring insights
public String[] uniquePermutations(String s) {
  // write your code here
}
s"aab"
expected["aab", "aba", "baa"]
Checking account…