String Permutations | Practice | GeeksforGeeks

Given a string S. The task is to find all permutations of a given string.
Example 1:
Input:
S = ABC
Output: ABC ACB BAC BCA CAB CBA 

Example 2:
Input:
S = ABSG
Output: ABGS ABSG AGBS AGSB ASBG ASGB
BAGS BASG BGAS BGSA BSAG BSGA GABS
GASB GBAS GBSA GSAB GSBA SABG SAGB
SBAG SBGA SGAB SGBA
 
Your Task:
This is a function problem. You only need to complete the function permutation that takes S as parameter and returns the list of permutations in lexicographically increasing order. The newline is automatically added by driver code.
Constraints:
1 ≤ size of string ≤ 5
Expected Time Complexity: O(N * N!), N = length of string.
Expected Auxiliary Space: O(1)
0
ankitparashxr2 weeks ago
Test Cases Passed:
10 / 10
Total Points Scored:
2/2
Total Time Taken:
0.31/1.26
Your Accuracy:
100%
 
class Solution{   public ArrayList<String> permutation(String s)   {       ArrayList<String> ans = new ArrayList<>();       permutation(s,"",ans);       Collections.sort(ans);       return ans;          }void permutation(String str,String ans,ArrayList<String> res){    if(str.length()==0)    {        res.add(ans);        return;    }    for(int i=0;i<str.length();i++)    {        char ch = str.charAt(i);        String left = str.substring(0,i);        String right = str.substring(i+1);        String ros = left+right;        permutation(ros,ans+ch,res);    }}}
0
shreyash9779664 weeks ago
//String Permutations

void solve(int i ,string s, vector<string>&ans){
        if(i>=s.size()){
            ans.push_back(s);
            return;
        }
        for(int j=i;j<s.size();j++){
            swap(s[j],s[i]);
            solve(i+1,s,ans);
            swap(s[j],s[i]);
        }
    }
    vector<string> permutation(string s)
    {
        //Your code here
        vector<string>ans;
        solve(0,s,ans);
        sort(ans.begin(),ans.end());
        return ans;
    }
0
patildhiren444 weeks ago
JAVA - 0.3
void permutation(String s, String per, ArrayList<String> al){
        if(s.length()==0){
            al.add(per);
            return;
        }
        
        for(int i=0; i<s.length(); i++){
            char ch = s.charAt(i);
            
            String ns = s.substring(0, i) + s.substring(i+1);
            
            permutation( ns, per+ch, al);
            
        }
    }
    
    public ArrayList<String> permutation(String s)
    {
        //Your code here
        ArrayList<String> al = new ArrayList<>();
        
        permutation(s, "", al);
        Collections.sort(al);
        return al;
    }
0
arnabkunduary961 month ago
Can someone please help why the Test Case is failing for this piece of code? It seems to be printing the possible combinations:
 
class Solution
{
    public ArrayList<String> permutation(String S)
    {
        //Your code here
        return getPermutations(S, new ArrayList<String>(), 0, "");
    }
    
    public ArrayList<String> getPermutations(String s, ArrayList<String> list, int n, String res)
    {
		if(n == s.length()) {
			list.add(res);
			return list;
		}
		
		for(int i = 0; i < s.length(); i++) {
			if(res.indexOf(s.charAt(i)) == -1) {
				getPermutations(s, list, n+1, res+ s.charAt(i));
			}
		}
		
		return list;
    }
	   
}
0
dipanshusharma93132 months ago
// java solution
class Solution{   static void perm(String str, String combinations, ArrayList<String> bh){       if(str.length()==0){          bh.add(combinations);          return;       }       for(int i=0;i<str.length();i++){           char ch = str.charAt(i);           String newstr = str.substring(0,i)+str.substring(i+1);           perm(newstr,combinations+ch,bh);       }   }   public ArrayList<String> permutation(String S)   {       //Your code here      ArrayList <String> di = new ArrayList<>();      perm(S,"",di);      Collections.sort(di);       return di;   }} 
0
sarthakgupta103 months ago
class Solution{
 public:
    //Complete this function
    void find(int index,string s,vector<string>&ans)
    {
        if(index==s.size())
        {
            ans.push_back(s);
           return;
        }
        for(int i=index;i<s.size();i++)
        {
            swap(s[i],s[index]);
            find(index+1,s,ans);
            swap(s[i],s[index]);
        }
        
    }
    
    vector<string> permutation(string S)
    {
        //Your code here
         vector<string>ans;
        find(0,S,ans);
        sort(ans.begin(),ans.end());
        return ans;
    }
};
//Simple c++ solution using the concept of recursion and backtracking
0
akkeshri140420013 months ago
void printpermutation(string S,string ans,vector<string> &v){       if(S.length()==0){           v.push_back(ans);           return;       }              for(int i=0;i<S.length();i++){           char ch=S[i];           string lpart=S.substr(0,i);           string rpart=S.substr(i+1);           string rqp=lpart+rpart;                      printpermutation(rqp,ans+ch,v);                  }   }      vector<string> permutation(string S)   {       //Your code here       vector<string>v;       string ans="";       printpermutation(S,ans,v);       sort(v.begin(),v.end());       return v;                 }
+1
tusharkumar040320023 months ago
C++ solution
class Solution{
    public:
    //Complete this function
    void help(string s,int low,int high,vector<string>&ans)
    {
        if(low==high)
        {
            ans.push_back(s);
            return ;
        }
        for(int i=low;i<=high;i++)
        {
            swap(s[low],s[i]);
            help(s,low+1,s.length()-1,ans);
            swap(s[low],s[i]);
        }
    }
    
    vector<string> permutation(string s)
    {
        vector<string>ans;
        help(s,0,s.length()-1,ans);
        sort(ans.begin(),ans.end()); //sort karna jaruri hai...order ke liye
        return ans;
    }
};
0
gurucharanchouhan173 months ago
Solution in java : || Recursive Solution :)
 
class Solution{   public static void check(String s1,String s2,List<String>l)   {       if(s1.length()==0)       {            l.add(s2);            return;       }       for(int i=0;i<s1.length();i++)       {           char ch=s1.charAt(i);           String left=s1.substring(0,i);           String right=s1.substring(i+1);           String ros=left+right;           check(ros,ch+s2,l);       }   }   public ArrayList<String> permutation(String S)   {       ArrayList<String>l=new ArrayList<String>();       check(S,"",l);       Collections.sort(l);       return l;   }   } 
0
koulikmaity4 months ago
la
 
String permutation problem solution
class Solution{
   public:
   
   void solve(string S, int index, vector<string> &ans)
   {
       // base case
       if( index > S.size() )
       {
           ans.push_back(s);
           return;
       }
       
       for(int i=index;i<S.size();i++)
       {
           swap(s[index] , s[i]);
           solve(S,index,ans);
           
       }
       
   }
   
   public:
   //Complete this function
   
   vector<string> permutation(string S)
   {
       //Your code here
       vector<string> ans;
       int index = 0;
       solve(S, index, ans);
       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.