All Subset I (without fixing size of subset, without order, without duplication)

DFS;

1.Link

2. 题目

Given a set of characters represented by a String, return a list containing all subsets of the characters.

Assumptions

  • There are no duplicate characters in the original set.

Examples

  • Set = "abc", all the subsets are [“”, “a”, “ab”, “abc”, “ac”, “b”, “bc”, “c”]

  • Set = "", all the subsets are [""]

  • Set = null, all the subsets are []

3. 思路

  1. DFS

  2. In recursion tree, each level = one char, each branch = choose or no choose that char

  3. use pos, position of char to select each different char in action set in each level to avoid duplication of solution

  4. Time: O(2^n), n = number of action/char in list, since each action can be "pick" or "not pick", it is binary tree

  5. Space: O(logn)

4. Coding

Last updated

Was this helpful?