-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPermutationsOfAString.cpp
More file actions
71 lines (56 loc) · 1.48 KB
/
Copy pathPermutationsOfAString.cpp
File metadata and controls
71 lines (56 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* GFG - My implementation
Given a string S. The task is to print all permutations of a given string.
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
*/
class Solution
{
public:
void generate_permutations(int i,string s,int l,vector<string> &ans)
{
if(i==l-1)
{
ans.push_back(s);
return;
}
// without swapping
generate_permutations(i+1,s,l,ans);
// swap with each
for(int j=i+1;j<l;j++)
{
swap(s[i],s[j]);
generate_permutations(i+1,s,l,ans);
swap(s[i],s[j]);
}
return;
}
vector<string>find_permutation(string S)
{
// Code here
vector<string> ans;
int l=S.length();
generate_permutations(0,S,l,ans);
sort(ans.begin(),ans.end());
return ans;
}
};