
Problem
Solution
Submissions
All Combinations of a String
Certification: Intermediate Level
Accuracy: 0%
Submissions: 0
Points: 10
Write a JavaScript program to generate all possible combinations of a given string. A combination includes all possible subsets of characters from the string, including the empty combination and the full string. Return an array containing all unique combinations.
Example 1
- Input: str = "abc"
- Output: ["", "a", "b", "c", "ab", "ac", "bc", "abc"]
- Explanation:
- Empty combination gives us "".
- Single character combinations give us "a", "b", "c".
- Two character combinations give us "ab", "ac", "bc".
- Full string combination gives us "abc".
- Total of 8 combinations (2^3) are generated.
- Empty combination gives us "".
Example 2
- Input: str = "ab"
- Output: ["", "a", "b", "ab"]
- Explanation:
- Empty combination is "".
- Single character combinations are "a" and "b".
- Two character combination is "ab".
- Total of 4 combinations (2^2) are possible.
- Empty combination is "".
Constraints
- 1 ≤ str.length ≤ 10
- str consists of unique lowercase English letters
- Time Complexity: O(2^n * n) where n is length of string
- Space Complexity: O(2^n * n) for storing all combinations
- The order of combinations in output array doesn't matter
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use recursive backtracking to generate combinations
- For each character, decide whether to include it or not
- Use two recursive calls - one including current character, one excluding it
- Build combinations by concatenating characters as you recurse
- Base case occurs when you've processed all characters
- Alternative approach: use bit manipulation to generate all 2^n combinations