Print all possible strings | Practice | GeeksforGeeks

Given a string str your task is to complete the function spaceString which takes only one argument the string str and  finds all possible strings that can be made by placing spaces (zero or one) in between them. 

For eg .  for the string abc all valid strings will be
                abc
                ab c
                a bc
                a b c

Example 1:
Input:
str = abc
Output: abc$ab c$a bc$a b c$

Example 2:
Input:
str = xy
Output: xy$x y$


Your Task:
Complete the function spaceString() which takes a character array as an input parameter and returns list of all possible answers. The driver code will print the all possible answer '$' separated
Expected Time Complexity: O(N * 2N)
Expected Auxiliary Space: O(N)

Constraints:
1<= length of string str <=10

Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
0
mestryruturaj3 weeks ago
//JAVA
class GfG {
    ArrayList<String> spaceString(String str) {
        ArrayList<String> res = new ArrayList<String>();
        helper(str, str, "", res);
        return res;
    }
    
    void helper(String str, String ip, String op, ArrayList<String> res) {
        if (str.length() == ip.length()) {
            op = "" + str.charAt(0);
            ip = str.substring(1);
            
            helper(str, ip, op, res);
        }
        else {
            if (ip.length() == 0) {
                res.add(op);
                return;
            }
            
            String op1 = op + " " + ip.charAt(0);
            String op2 = op + ip.charAt(0);
            ip = ip.substring(1);
            
            helper(str, ip, op2, res);
            helper(str, ip, op1, res);
        }
    }
}
0
shrustis1761 month ago
C++ solution
 
void solve(string ip, string op, vector<string> &v){   if(ip.length() == 0)   {       v.push_back(op);       return;   }      string op1=op;   string op2=op;      op1.push_back(' ');   op1.push_back(ip[0]);      op2.push_back(ip[0]);   ip.erase(ip.begin()+0);      solve(ip,op1,v);   solve(ip,op2,v);     }
vector<string>  spaceString(char str[]){//Your code here   string ip,op;   vector<string>v;  // int l=str.length();     ip=str;      op.push_back(ip[0]);   ip.erase(ip.begin()+0);      solve(ip,op,v);   reverse(v.begin(), v.end());   return v;}
+1
akanksha00122 months ago
void recString(string S,int i, string s, vector<string>& a){
    if(S.size()<=i){
        a.push_back(s);
        return;
    }
    recString(S,i+1,s+S[i],a);//no space
    recString(S,i+1,s+" "+S[i],a);//add space
}

vector<string>  spaceString(char str[])
{
//Your code here
    vector<string> ans;
    string S="";
    for(int i=0;str[i]!='\0';i++)
        S+=str[i];
    string s;
    s+=S[0];//initailize with S[0]
    recString(S,1,s,ans);//start from index 1
    return ans;
} 
0
blank551k2 months ago
// int n=1;void storestring(vector<string>& a,string s,int i){   // cout<<n<<" ";   // ++n;   if(i>=s.size()){       // cout<<s<<endl;       a.push_back(s);       return ;   }         storestring(a,s,i+1);   storestring(a,(s.substr(0,i)+" "+s.substr(i)),i+2);}
vector<string>  spaceString(char str[]){   string s="";   vector<string> a;   for(int i=0;str[i]!='\0';++i){       s.push_back(str[i]);   }   storestring(a,s,1);   return a;}
0
madhumita1310002 months ago
void recur(vector<string>&s,string st,char str[],int i,int l){   if(i==l)   {       s.push_back(st);       //s.push_back("$");       return;   }   else   {       recur(s,st+str[i],str,i+1,l);       if(i<l-1)       {            recur(s,st+str[i]+" ",str,i+1,l);       }             }}
vector<string>  spaceString(char str[]){//Your code hereint i=0;//int l=str.length();int j=0,l=0;/*while(str[j]){   j++;   l++;}*/vector<string> s;string st="";recur(s,st,str,i,l);return s;}
0
ppt892 months ago
Java Solution Recursive Approach
 
class GfG
{
   ArrayList<String> spaceString(String str)
   {
       // Your code here
       ArrayList<String> res = new ArrayList<>();
       if(str.length() == 0) return res;
       
       String op = ""+str.charAt(0);
       str = str.substring(1);
       return solve(str, op, res);
       
   }
   ArrayList<String> solve(String ip, String op, ArrayList<String> res){
       if(ip.length() == 0){
           res.add(op);
           return res;
       }
       String op1 = op;
       String op2 = op;
       op1 += " "+ip.charAt(0);
       op2 += ip.charAt(0);
       
       ip = ip.substring(1);
       
        solve(ip, op2, res);
       solve(ip, op1, res);
      
       
       return res;
   }
}
0
ankurjeesingh3102 months ago
My JavaScript Code:::::---→
class Solution {   spaceString(str){          if(str.length===1){         return [str];     }                    let ch = str.charAt(0);     let res = str.substring(1);          let subStr = this.spaceString(res);          let myres = [];
         for(let i =0; i<subStr.length; i++){             myres.push(ch+subStr[i]);         }         for(let i =0; i<subStr.length; i++){             myres.push(ch+" "+subStr[i]);         }                   return myres;             }}
 
 
+2
pranavtripathi20013 months ago
void subsets(string input,int index,string output,vector<string> &v){   if(index==input.size()){       v.push_back(output);       return;   }           //Dont pick the space       string op1=output+input[index];       string op2=output+" "+ input[index];       subsets(input,index+1,op1,v);              //pick the space       subsets(input,index+1,op2,v);                    }
vector<string>  spaceString(char str[]){   string input = str;     vector<string>v;      string output="";    output+=input[0];     subsets(input,1,output,v);     return v;         } 
+1
sachinkartik1663 months ago
class GfG{   ArrayList<String> spaceString(String str)   {       ArrayList<String> list=new ArrayList<>();              rec(list,str,0,"");              return list;   }         public void rec(ArrayList<String> list,String str,int i,String s){              if(str.length()==i){                      list.add(s);           return;       }        rec(list,str,i+1,s+str.charAt(i));        if(i==0){          return;        }        rec(list,str,i+1,s+" "+str.charAt(i));               }   } 
0
alexluthner77
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.