Permutation with Spaces | Practice | GeeksforGeeks

Given a string you need to print all possible strings that can be made by placing spaces (zero or one) in between them. The output should be printed in sorted increasing order of strings
Example 1:
Input:
S = "ABC"
Output: (A B C)(A BC)(AB C)(ABC)
Explanation:
ABC
AB C
A BC
A B C
These are the possible combination of "ABC".
 
Example 2:
Input:
S = "AB"
Output: (A B)(AB)


Your Task:  
You don't need to read input or print anything. Your task is to complete the function permutation() which takes the string S as input parameters and returns the sorted array of the string denoting the different permutation (DON'T ADD '(' and ')' it will be handled by the driver code only).

Expected Time Complexity: O(2^n)
Expected Auxiliary Space: O(1)
 
CONSTRAINTS:
1 < |S| < 10
S only contains lowercase and Uppercase English letters.
0
vishalsavade2 days ago
// This code is contributed by SK
//Easiest Code 
void addspace(string s, string sp, int l, vector<string> &v){
    if(l == s.size()){
        v.push_back(sp);
        return;
    }
    sp.push_back(s[l]);
    if(l+1 != s.size())
       addspace(s, sp+" ", l+1, v);
    addspace(s, sp, l+1, v);
}
vector<string> permutation(string s){
   // Code Here
   vector<string> v;
   addspace(s, "", 0, v);
   return v;
}
0
adarshgupta4012 weeks ago
 Easy Java Recursive Soln
class Solution{ 
    ArrayList<String> permutation(String S){
        // Code Here
        ArrayList<String> wraplist = new ArrayList<>();
        String op = ""; 
        op += S.charAt(0);
        S= S.substring(1);
        permutationSpaces(wraplist , S, op);
        return wraplist;
    }
    void permutationSpaces(ArrayList<String> wraplist, String S, String op){
        
        if(S.length() == 0){ 
            wraplist.add(op);
            return;
        }
        String op1 = op, op2 = op;
        op1 += " "+S.charAt(0);
        op2 += S.charAt(0);
        
        S= S.substring(1);
            
        permutationSpaces(wraplist, S, op1); 
        permutationSpaces(wraplist, S, op2);
        
        return; 
    }
    
}
+2
sainiumesh6661 month ago
Aditya verma sir OP!!!!!!
 
 void solve(vector<string>&ans,string S,string op){    if(S.size()==0)   {       ans.push_back(op);       return;   }   string op1=op;   string op2=op;   op1.push_back(' ');   op1.push_back(S[0]);   op2.push_back(S[0]);   S.erase(S.begin()+0);   solve(ans,S,op1);   solve(ans,S,op2);   }  vector<string> permutation(string S){     string op="";     op.push_back(S[0]);     S.erase(S.begin()+0);     vector<string>ans;     solve(ans,S,op);     return ans;  } 
0
ajay7yadav951 month ago
//javaScript   program code is fine ----→by A7
class Solution { permutation(s) {     let l = s.length;     let temp = new Array(2 * l);     temp[0] = s[0];   //fixed element no movement we start from 2nd index     let i = 1;     let j = 1;    function printPermutation(s,i,j,temp,l){        // base case       if(i==l){           temp.join("");           return;       }        // put element  not 0 indexing start with first index becouse my i is 1        temp[j]=s[i];        printPermutation(s,i+1,j+1,temp,l)        // put space        temp[j] = " ";     // put space         temp[j+1] = s[i];  // move and put element in next index        printPermutation(s,i+1,j+2,temp,l)    }    printPermutation(s,i,j,temp,l);    return temp; }}
+2
kashyapjhon1 month ago
C++ Solution Using Input/Output Method Time=(0.0/1.5) EASY:
void help(vector<string> &u,string S,string out){       if(S.length()==1){           u.push_back(out+S);           return;       }       string out1=out;       string out2=out;       out1.push_back(S[0]);       out2.push_back(S[0]);       out2.push_back(' ');       S.erase(S.begin()+0);       help(u,S,out1);       help(u,S,out2);       return;   }   vector<string> permutation(string S){       // Code Here       vector<string> u;       string out="";       help(u,S,out);       reverse(u.begin(),u.end());       return u;   }
0
sharmahimanshu650181 month ago
//Jai BABA Ki 🙏
void fun(string &s,int idx,vector<string>&ds)
    {
        int n=s.size();
        if(idx>=n)
        {
            ds.push_back(s);
            return ;
        }
        s.insert(idx, " ");
        fun(s,idx+2,ds);
        s.erase(s.begin() + idx);
        fun(s,idx+1,ds);
    }
    
    vector<string> permutation(string s)
    {
        int n=s.size();
        vector<string>ds;
        fun(s,1,ds);
        return ds;
    }
+2
amanasati11 month ago
Easiest Backtracking Solution
what we will do is fix the A, then pick B or _B similarly C or _C and so on backtrack up till end. 
class Solution:
    def solve(self,ip,op,ls):
        if len(ip)==0:
            ls.append(op)
            return
        # not picking _ with next input
        op1=op+ip[0]
        # picking _ with next input
        op2=op+" "+ip[0]
        self.solve(ip[1:],op1,ls)
        self.solve(ip[1:],op2,ls)
        
    def permutation (self, S):
        # code here
        op=S[0]
        ls=[]
        self.solve(S[1:],op,ls)
        ls.sort()
        return ls
0
7579209537a2 months ago
class Solution{      ArrayList<String> permutation(String S){   ArrayList<String> j = new ArrayList<>();   j=  printSpace(S , "");   Collections.sort(j);     return j;   }   ArrayList<String> printSpace( String ori , String ans ){        ArrayList<String> s = new  ArrayList<>();                if(ori.length() == 1){                     s.add(ori);           return s;       }              for(int i = 0 ; i<ori.length();i++){          char  curr = ori.charAt(i);          printSpace(ori.substring(i+1), ans+curr+" ");                     printSpace(ori.substring(i+1), ans+curr);                            }     return s;          }   }
whats wrong with this solution can anyone please help
0
phantom3332 months ago
    vector<string> ans;      void solve(string ip,string op){       if(ip==""){           ans.push_back(op);           return;       }              string op1=op;       string op2=op;       op1.push_back(ip[0]);       op2.push_back(' ');       op2.push_back(ip[0]);       ip.erase(ip.begin()+0);       solve(ip,op1);       solve(ip,op2);
   }
 
vector<string> permutation(string S){              string ip=S;       string op="";              op.push_back(ip[0]);       ip.erase(ip.begin()+0);              solve(ip,op);       sort(ans.begin(),ans.end());              return ans;   }
0
bhardwajji2 months ago
void solve(string in,string op ,vector<string>&v){
        

        if(in.length() == 0){
            v.push_back(op);

            return;
        }
        
        string op1 = op + " " + in[0];
        string op2 = op + in[0];
        in.erase(in.begin() +0);
        
        solve(in,op1,v);
        solve(in,op2,v);
        
        return;
        
    }

    vector<string> permutation(string in){
        // Code Here
        vector<string>v;
        string op="";
        op+=in[0];
        in.erase(in.begin() +0);
        solve(in,op,v);
        sort(v.begin() ,v.end());
        return v;
        
    }
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.