Permutations of a given string | Practice | GeeksforGeeks

Given a string S. The task is to print all permutations of a given string in lexicographically sorted order.
 
Example 1:
Input: ABC
Output:
ABC ACB BAC BCA CAB CBA
Explanation:
Given string ABC has permutations in 6 
forms as ABC, ACB, BAC, BCA, CAB and CBA .

Example 2:
Input: 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
Explanation:
Given string ABSG has 24 permutations.

 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order.
 
Expected Time Complexity: O(n! * n)
Expected Space Complexity: O(n)
 
Constraints:
1 <= length of string <= 5
 
 
0
rahulsharmaec20b1339in 2 hours
can anyone help me, im getting segmentation fault int v[i]==s condition
bool check(vector<string> &v, string &s)
	{
	    for(int i=0; v.size();i++)
	    {
	        if(v[i]==s)
	        return 0;
	    }
	    return 1;
	}
	vector <string> solun(string &s, vector<string> &v ,int pos)
	{
	    if(pos>=s.size())
	    {
	        if(check(v,s) || v.empty()){
	         v.push_back(s);   
	        }
	    }
	    for(int i=pos;i<s.size();i++)
	    {
	        swap(s[i],s[pos]);
	        solun(s,v,pos+1);
	        swap(s[i],s[pos]);
	    }
	    return v;
	}
		vector<string>find_permutation(string s)
		{
		    vector <string> v;
		    solun(s,v,0);
		    sort(v.begin(),v.end());
		    return v;
		    
		}
0
kanein 2 hours
vector<string>ans;    void solve(vector<int>&vis,string S,string path){        if(path.length()==S.length()){            ans.push_back(path);            return ;        }        for(int i=0;i<S.length();i++){            if(vis[i]==0){                vis[i]=1;                solve(vis,S,path+S[i]);                vis[i]=0;            }                    }    } vector<string>find_permutation(string S) {     // Code here there     vector<int>vis(S.length(),0);     solve(vis,S,"");     sort(ans.begin(),ans.end());     vector<string>res;     res.push_back(ans[0]);     for(int i=1;i<ans.size();i++){         if(ans[i-1]!=ans[i])res.push_back(ans[i]);     }     return res; }
0
kane
This comment was deleted.
0
abhishekips07in 1 hour
Python Approach.
class Solution:
    def find_permutation(self, S):
        res=set()
        visit=[False]*len(S)
        def dfs(i,ans):
            if len(ans)==len(S):
                res.add(ans)
                return
            if i>len(S):
                return
            for j in range(len(S)):
                if visit[j]==True:
                    continue
                visit[j]=True
                dfs(j+1,ans+S[j])
                visit[j]=False
        dfs(0,"")
        res=list(res)
        res.sort()
        return res
0
nitesh8002 hours ago
class solution
{public:    unordered_set<string>st;    void solve(string s, int i, int n, vector<string>&v)    {        if(i+1 == n)        {            if(st.find(s) == st.end())            {            v.push_back(s);            st.insert(s);            }            return;        }        for(int j=i;j<n;j++)        {            swap(s[i],s[j]);            solve(s,i+1,n,v);            swap(s[i],s[j]);        }    } vector<string>find_permutation(string s) {     vector<string>v;     int n=s.length();     solve(s,0,n,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.
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.