-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringPermutation
More file actions
62 lines (33 loc) · 919 Bytes
/
stringPermutation
File metadata and controls
62 lines (33 loc) · 919 Bytes
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
// C++ program to print all permutations with duplicates
#include<bits/stdc++.h>
#include<algorithm>
using namespace std;
// using rotate() in STL
void permutate(string str,string out){
if(str.size() == 0){
cout<<out<<endl;
return;
}
// One by one move all characters at the beginning of out
for(int i=0;i<str.size();i++){
permutate(str.substr(1),out + str[0]);
rotate(str.begin(),str.begin()+1,str.end());
}
}
// using next_permutation()
void permute(string str){
// sort the string in lexicographically ascending order
sort(str.begin(),str.end());
// printing permutation while there is next permutation
do{
cout<<str<<endl;
}while(next_permutation(str.begin(),str.end()));
}
int main(){
string str = "CBA";
permute(str);
cout<<endl<<endl;
string str1 = "ABC";
permutate(str, "");
return 0;
}