Print all possible strings that can be made by placing spaces - GeeksforGeeks

29 Apr, 2021
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.  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to print all strings that can be// made by placing spaces#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;    }}  // Driver codeint main(){    string str = "ABC";    printSubsequences(str);    return 0;}
// Java program to print all strings that can be// made by placing spacesimport 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();    }}  // Driver codepublic static void main(String[] args){    String str = "AB";    printSubsequences(str);}}  /* This code is contributed by Mr. Somesh Awasthi */
# Python 3 program to print all strings # that can be made by placing spacesfrom 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 = "")  # Driver codeif __name__ == '__main__':    str = "ABC"    printSubsequences(str)  # This code is contributed by# Sanjit_Prasad
// C# program to print all strings// that can be made by placing spacesusing System;  class GFG {          // Function to print all subsequences    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();        }    }          // Driver code    public static void Main()    {        String str = "ABC";        printSubsequences(str);    }}  // This code is contributed by shiv_bhakt.
<?php// PHP program to print // all strings that can be// made by placing spaces  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";    }}  // Driver code$str = "ABC";printSubsequences($str);  // This code is contributed by ajit?>
<script>  // Javascript program to print all strings// that can be made by placing spaces  // Function to print all subsequencesfunction 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>");    }}  // Driver codelet str = "ABC";printSubsequences(str);  // This code is contributed by rameshtravel07</script>
Output : 
ABC
A BC
AB C
A B C
Asked in: AmazonThis article is contributed by Jebasingh and Ninja. 
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Vishal_Khoda
jit_t
Sanjit_Prasad
rameshtravel07
Amazon
Backtracking
Combinatorial
Recursion
Strings
Amazon
Strings
Recursion
Combinatorial
Backtracking
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Backtracking | Introduction
Subset Sum | Backtracking-4
m Coloring Problem | Backtracking-5
Print all paths from a given source to a destination
Hamiltonian Cycle | Backtracking-6
Permutation and Combination in Python
Program to calculate value of nCr
itertools.combinations() module in Python to print all possible combinations
Combinational Sum
Count ways to reach the nth stair using step 1, 2 or 3