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++
#include <bits/stdc++.h>
using namespace std;
void printSubsequences(string str)
{
int n = str.length();
unsigned int opsize = pow (2, n - 1);
for ( int counter = 0; counter < opsize; counter++) {
for ( int j = 0; j < n; j++) {
cout << str[j];
if (counter & (1 << j))
cout << " " ;
}
cout << endl;
}
}
int main()
{
string str = "ABC" ;
printSubsequences(str);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void printSubsequences(String s)
{
char [] str= s.toCharArray();
int n = str.length;
int opsize = ( int )(Math.pow( 2 , n - 1 ));
for ( int counter = 0 ; counter < opsize; counter++) {
for ( int j = 0 ; j < n; j++) {
System.out.print(str[j]);
if ((counter & ( 1 << j)) > 0 )
System.out.print( " " );
}
System.out.println();
}
}
public static void main(String[] args)
{
String str = "AB" ;
printSubsequences(str);
}
}
|
Python3
from math import pow
def printSubsequences( str ):
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 ( "\n" , end = "")
if __name__ = = '__main__' :
str = "ABC"
printSubsequences( str )
|
C#
using System;
class GFG {
static void printSubsequences(String s)
{
char [] str= s.ToCharArray();
int n = str.Length;
int opsize = ( int )(Math.Pow(2, n - 1));
for ( int counter = 0; counter < opsize;
counter++)
{
for ( int j = 0; j < n; j++)
{
Console.Write(str[j]);
if ((counter & (1 << j)) > 0)
Console.Write( " " );
}
Console.WriteLine();
}
}
public static void Main()
{
String str = "ABC" ;
printSubsequences(str);
}
}
|
PHP
<?php
function printSubsequences( $str )
{
$n = strlen ( $str );
$opsize = pow(2, $n - 1);
for ( $counter = 0;
$counter < $opsize ;
$counter ++)
{
for ( $j = 0; $j < $n ; $j ++)
{
echo $str [ $j ];
if ( $counter & (1 << $j ))
echo " " ;
}
echo "\n" ;
}
}
$str = "ABC" ;
printSubsequences( $str );
?>
|
Javascript
<script>
function printSubsequences(s)
{
let str= s.split( '' );
let n = str.length;
let opsize = Math.pow(2, n - 1);
for (let counter = 0; counter < opsize; counter++)
{
for (let j = 0; j < n; j++)
{
document.write(str[j]);
if ((counter & (1 << j)) > 0)
document.write( " " );
}
document.write( "</br>" );
}
}
let str = "ABC" ;
printSubsequences(str);
</script>
|
Output
ABC
A BC
AB C
A B C
Time complexity : O(n*2n-1)
Auxiliary Space : O(1)
Asked in: Amazon
/p>
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
12 Jan, 2023
Like Article
Save Article