Open In App

Print all possible strings that can be made by placing spaces using Power Set

Improve
Improve
Like Article
Like
Save
Share
Report

Given a string you need to print all possible strings that can be made by placing spaces (zero or one) in between them 

Examples : 

Input :  str[] = "ABC"
Output : ABC
         AB C
         A BC
         A B C

Input : str[] = "ABCD"
Output : ABCD
         A BCD
         AB CD
         A B CD
         ABC D
         A BC D
         AB C D
         A B C D

If we take a  closer look, we can notice that this problem boils down to Power Set problem. We basically need to generate all subsets where every element is a different space.  

Implementation:

C++





Java





Python3





C#





PHP





Javascript





Output

ABC
A BC
AB C
A B C

Time complexity : O(n*2n-1
Auxiliary Space : O(1)

Asked in: Amazon
/p>

 



Last Updated : 12 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads