Given a string you need to print all possible strings that can be made by placing spaces (zero or one) in between them
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.
# Python 3 program to print all strings
# that can be made by placing spaces
from math import pow
n = len(str)
opsize = int(pow(2, n – 1))
for counter in range(opsize):
for j in range(n):
print(str[j], end = “”)
if (counter & (1 << j)): print(" ", end = "") print(" ", end = "") # Driver code if __name__ == '__main__': str = "ABC" printSubsequences(str) # This code is contributed by # Sanjit_Prasad [tabby title="C#"]
ABC A BC AB C A B C
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
This article is attributed to GeeksforGeeks.org