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
samyakm511 week ago
Python solution in 0.03/1.02 secs
    def solve(self,n,ip,op,l):
        if len(op)==0:
            l.append(ip)
            return
        if len(op)==n:
            self.solve(n,ip+op[0],op[1:],l)
        else:
            self.solve(n,ip+' '+op[0],op[1:],l)
            self.solve(n,ip+op[0],op[1:],l)
            
    def permutation (self, S):
        # code here
        
        n=len(S)
        l=[]
        self.solve(n,'',S,l)
        return l
        
        
0
mrpalashbharati2 weeks ago
C++ || Run Time : 0.01
vector<string> v;
    
    void solve(string s, string output){
        
        if(s.empty()){
            v.push_back(output);
            return;
        }

        output.push_back(' ');
        output.push_back(s[0]);
        solve(s.substr(1),output);
        
        output.pop_back();
        output.pop_back();
        output.push_back(s[0]);
        solve(s.substr(1),output);

    }
    
    vector<string> permutation(string s){
        // Code Here
        // vector<string> v;
        
        string output="";
        output.push_back(s[0]);
        
        solve(s.substr(1),output);
        
        return v;
    }
0
hellosoham20012 weeks ago
Python Solution - 0.06 seconds :
 
class Solution:
    def permutation (self, S):
 
        ip=S
        op=''
        ans=[]
        
        def solve(ip,op):
            if len(ip)==0:
                ans.append(op)
                return 
            op1=op
            op2=op
            
            op1=op1+ip[0]
            op2=op2+ip[0]+' '
 
            ip=ip[1:]
            solve(ip,op1)
            solve(ip,op2)
              
        solve(ip,op)
 
        for i in ans:
            if i[-1]==' ':
                ans.remove(i)
            
        ans.sort()
        return ans
0
piyush9534972 weeks ago
Java 0.36/1.48
ArrayList<String> permutation(String S){              if(S.length() == 0){           ArrayList<String> a = new ArrayList<>();           a.add("");           return a;       }
       char ch = S.charAt(0);       ArrayList<String> b = permutation(S.substring(1));
       ArrayList<String> ans = new ArrayList<>();
       for(String s1 : b){           if(!s1.isEmpty())               ans.add(ch + " " + s1);       }
       for(String s1 : b){           ans.add(ch + s1);       }       return ans;   }
0
damamrajeev2 weeks ago
Easy solution using c++ - 0.01 seconds
 
void helper(string s,string out,vector<string> &res){
        if(s.length()==0){
            res.push_back(out);
            return;
        }
        // including space
        helper(s.substr(1),out+" "+s[0],res);
        // without space
        helper(s.substr(1),out+s[0],res);
    }

    vector<string> permutation(string s){
        // Code Here
        
        vector<string> out;
        // if string length is 0 then no need to do anything
        if(s.length()==0){
            return out;
        }
        // store is just used to convert char to string
        string store="";
        store+=s[0];
        // passing first char of string inorder to reduce complication in recursive calls
        // if we just pass s[0] to helper functin in place of store we will be passing character instead of string
        helper(s.substr(1),store,out);
        return out;
    }
 
 
0
rajsinghrathour02 weeks ago
void solve(string S, int index,vector<string>&s,string t){   if(index==S.length())   {       s.push_back(t);       return;   }   t+=S[index];   if(index<S.length()-1){t+=" ";   solve(S,index+1,s,t);}   if(index!=S.length()-1)t.pop_back();   solve(S,index+1,s,t);}
   vector<string> permutation(string S){       vector<string>s;       string t="";       solve(S,0,s,t);      // sort(s.begin(),s.end());       return s;       // Code Here   }};
0
roytanupriya212 weeks ago
Using Aditya Verma's Approach:
 
#include<bits/stdc++.h>
using namespace std;

vector<string> subset(string ip, string op, vector<string> &ans){
    //Base Condition
    if(ip.size()==0){
        ans.push_back(op);
        return ans;
    }
    //Hypothesis
    string op1=op;
    string op2=op;
    op2.push_back('_');
    op2.push_back(ip[0]);
    op1.push_back(ip[0]);
    ip.erase(ip.begin()+0);
    //Induction
    subset(ip,op1,ans);
    subset(ip,op2,ans);
    return ans;
}

int main()
{
    string ip;
    cin>>ip;
    string op="";
    op.push_back(ip[0]);
    ip.erase(ip.begin()+0);
    vector<string> ans;
    subset(ip,op,ans);
    for(int i=0;i<ans.size();i++){
        cout<<ans[i]<<" ";
    }
    return 0;
}
+2
avinashdhn19043 weeks ago
public:
void solve(vector<string>&ans,string s,int n,int i,string temp){
    if(i==n){
        ans.push_back(temp);
        return;
    }
    if(s[i]==' '){
        solve(ans,s,n,i+1,temp+' ');
        solve(ans,s,n,i+1,temp);
        return;
    }
    else {
        solve(ans,s,n,i+1,temp+s[i]);
        return;
    }
    return;
}

    vector<string> permutation(string S){
        vector<string>ans;
        string s;
        int n=S.length();
        for(int i=0;i<n;i++){
            s+=S[i];
            if(i!=n-1)s+=' ';
            }
         
            solve(ans,s,s.length(),0,"");
            return ans;
    }
+1
lasthoneybadger4 weeks ago
C++
	vector<string> res;
    void perm(string &S, int i, string curr){
        if(i == S.size()){
            res.push_back(curr);
            return;
        }
        if(i)
        perm(S, i + 1, curr + " " + S[i]);
        perm(S, i + 1, curr + S[i]);
        
    }
    vector<string> permutation(string S){
        // Code Here
        perm(S, 0, "");
        return res;
    }
0
swapniltayal1 month ago
    void perWithSpace(string &s, vector<string> &ans, string ds, int index){       if(index == s.size()){           ans.push_back(ds);           return;       }       perWithSpace(s, ans, ds+s[index], index+1);       if(index != s.size()-1){           perWithSpace(s, ans, ds+s[index] + " ", index+1);       }   }
   vector<string> permutation(string S){       // Code Here       vector<string> ans;       string ds;       perWithSpace(S, ans, ds, 0);       reverse(ans.begin(), ans.end());       return ans;   }
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.