Problem · String

Sorted Subsets of a String

Learn this problem
MediumMicrosoft logoMicrosoftFULLTIMEPHONE SCREEN
See Microsoft hiring insights

Problem statement

Given a string s containing distinct lowercase English letters, return every subset of its characters.

Represent each subset as a string whose characters are in ascending order. Include the empty subset as the empty string, and return the complete list in lexicographic order.

Function

generateSubsets(s: String) → String[]

Examples

Example 1

s = "abc"return = ["","a","ab","abc","ac","b","bc","c"]

The eight strings represent all subsets. Every string is internally sorted, and the list is lexicographically sorted.

Example 2

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

The input order does not affect the canonical representation or result ordering.

Example 3

s = ""return = [""]

The empty string has exactly one subset: the empty subset.

Constraints

  • 0 <= s.length <= 16
  • s contains distinct lowercase English letters.

More Microsoft problems

drafts saved locally
public String[] generateSubsets(String s) {
    // Write your solution here
}
s"abc"
expected["", "a", "ab", "abc", "ac", "b", "bc", "c"]
checking account