Introduction
The Permutation with Spaces problem is a classic recursion question that helps build a strong understanding of decision-making and backtracking patterns.
Instead of generating permutations by rearranging characters, this problem focuses on inserting spaces between characters in all possible ways.
What makes this problem powerful is its decision tree structure, which you’ve already visualized perfectly. In this article, we will directly connect that intuition with code.
Link of Problem: GeeksforGeeks – Permutation with Spaces
Problem Statement
Given a string s, generate all possible strings by placing:
- Either a space
- Or no space
between every pair of characters.
Return all results in sorted order.
Example
Input:
s = "ABC"
Output:
A B C
A BC
AB C
ABC
Understanding Your Decision Tree (Very Important)

Two Choices at Each Step:
❌ Do NOT add space before the character
✔️ Add space before the character
Mapping Tree
From diagram:
- At B:
"AB"→ no space"A B"→ space- At C:
- From
"AB": "ABC""AB C"- From
"A B": "A BC""A B C"
Final Output (Leaf Nodes)
As shown in your diagram:
ABC, AB C, A BC, A B C
📌 This is exactly what recursion generates.
Key Insight
At every index (except first), we have:
So total combinations:
Approach: Recursion + Decision Making
Idea
- Fix the first character
- For every next character:
- Add space + character
- Add character directly
- Continue recursively
Java Code with Detailed Comments
Step-by-Step Execution (Using Your Tree)
For "ABC":
- Start →
"A" - At
"B": "AB""A B"- At
"C": "ABC","AB C""A BC","A B C"
Exactly matches your decision tree leaf nodes ✅
Complexity Analysis
- Time Complexity: O(2ⁿ)
- Space Complexity: O(2ⁿ)
Why This Approach Works
- Recursion explores every possible choice
- Each level = one character
- Each branch = decision (space / no space)
- Leaf nodes = final answers
Key Takeaways
- This is a binary decision recursion problem
- Always identify:
- Choices
- Base condition
- Your decision tree = direct blueprint of recursion
- Same pattern applies to:
- Subsets
- Binary choices problems
Conclusion
The Permutation with Spaces problem becomes extremely simple once the decision tree is understood—and your diagram already captures that perfectly.
The recursion directly follows the same structure:
- Every branch = one decision
- Every leaf = one answer
Master this pattern, and you’ll find many recursion problems much easier to solve.




