Introduction
The Power Set problem for strings is a classic question in recursion and backtracking, frequently asked in coding interviews and platforms like GeeksforGeeks.
In this problem, instead of numbers, we deal with strings and generate all possible subsequences (not substrings). This makes it slightly more interesting and practical for real-world applications like pattern matching, text processing, and combinatorics.
In this article, we will cover:
- Intuition behind subsequences
- Recursive (backtracking) approach
- Sorting for lexicographical order
- Alternative approaches
- Complexity analysis
Problem Statement
Given a string s of length n, generate all non-empty subsequences of the string.
Requirements
- Return only non-empty subsequences
- Output must be in lexicographically sorted order
Examples
Example 1
Input:
s = "abc"
Output:
a ab abc ac b bc c
Example 2
Input:
s = "aa"
Output:
a a aa
Subsequence vs Substring (Important)
- Substring: Continuous characters
- Subsequence: Characters can be skipped
Example for "abc":
Key Insight
For every character, we have two choices:
So total subsequences:
We generate all and then remove the empty string.
Approach 1: Recursion (Backtracking)
Intuition
- At each index:
- Skip the character
- Include the character
- Build all combinations recursively
Java Code (With Explanation)
Step-by-Step Dry Run (s = "abc")
Final Output (After Sorting)
Approach 2: Bit Manipulation
Intuition
Each subsequence can be represented using binary numbers:
Code
Approach 3: Iterative (Expanding List)
Idea
- Start with empty list
- For each character:
- Add it to all existing subsequences
Code
Complexity Analysis
- Time Complexity: O(n × 2ⁿ)
- Space Complexity: O(n × 2ⁿ)
Why?
- Total subsequences = 2ⁿ
- Each subsequence takes O(n) to build
Why Sorting is Required
The recursion generates subsequences in random order, so we sort them:
This ensures lexicographical order as required.
Key Takeaways
- This is a power set problem for strings
- Each character → 2 choices
- Recursion = most intuitive approach
- Bit manipulation = most optimized thinking
- Always remove empty string if required
Common Interview Variations
- Subsets of array
- Permutations of string
- Combination sum
- Subsequence with conditions
Conclusion
The Power Set problem is a fundamental building block in recursion and combinatorics. Once you understand the include/exclude pattern, you can solve a wide range of problems efficiently.
Mastering this will significantly improve your ability to tackle backtracking and decision tree problems.
Frequently Asked Questions (FAQs)
1. Why is the empty string removed?
Because the problem requires only non-empty subsequences.
2. Why is time complexity O(n × 2ⁿ)?
Because there are 2ⁿ subsequences and each takes O(n) time to construct.
3. Which approach is best?
- Recursion → best for understanding
- Bit manipulation → best for optimization




