-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathTricky Permutations.cpp
52 lines (46 loc) · 1.31 KB
/
Tricky Permutations.cpp
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
/* Coding Blocks */
/* Title - Tricky Permutations */
/* Created By - Akash Modak */
/* Date - 23/7/2020 */
// Given a string containing duplicates, print all its distinct permutations such that there are no duplicate permutations and all permutations are printed in a lexicographic order.
// Input Format
// The first and only line of the test case contains the input string.
// Constraints
// Length of the string <= 8
// Output Format
// Print all the distinct permutations in a lexicographic order such that each permutation is in a new line. Note that there should not be any duplicate permutations.
// Sample Input
// ABA
// Sample Output
// AAB
// ABA
// BAA
// Explanation
// The possible permutations for the given string are { "AAB" , "AAB" , "ABA" , "BAA" } . We skip the repeating "AAB" permutation and only print it in once. Also we print the final output in lexicographical order.
#include<iostream>
#include<set>
#include<cstring>
using namespace std;
set<string> s;
void permute(char *inp,int i){
if(inp[i]=='\0')
{
s.insert(inp);
return;
}
for(int j=i;inp[j]!='\0';j++){
swap(inp[i],inp[j]);
permute(inp,i+1);
swap(inp[i],inp[j]);
}
return;
}
int main() {
char inp[10000];
cin>>inp;
permute(inp,0);
for(auto x: s){
cout<<x<<endl;
}
return 0;
}