Skip to content
Onsite

All Subsequences of a String

MediumP2Coding

Return every distinct non-empty subsequence of the string s, in any order.

A subsequence is formed by deleting zero or more characters without changing the order of the rest. If different choices of positions produce the same string (for example in "aa"), include that string once.

Example 1

Input
s = "abc"
Output
["abc","ab","ac","a","bc","b","c"]
Why
Seven non-empty subsequences, in any order.

Example 2

Input
s = "aa"
Output
["aa","a"]
Why
Choosing either `a` gives the same string, which is listed once.

Example 3

Input
s = ""
Output
[]

Constraints

  • 0 <= s.length <= 12
  • s contains lowercase English letters.

Hints

Run checks the 3 visible tests, Submit all 9.