forked from blank-27/C-coding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutation.cpp
More file actions
39 lines (35 loc) · 889 Bytes
/
permutation.cpp
File metadata and controls
39 lines (35 loc) · 889 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
// C++ program to print all permutations with
// duplicates allowed using rotate() in STL
#include <bits/stdc++.h>
using namespace std;
// Function to print permutations of string str,
// out is used to store permutations one by one
void permute(string str, string out)
{
// When size of str becomes 0, out has a
// permutation (length of out is n)
if (str.size() == 0)
{
cout << out << endl;
return;
}
// One be one move all characters at
// the beginning of out (or result)
for (int i = 0; i < str.size(); i++)
{
// Remove first character from str and
// add it to out
permute(str.substr(1), out + str[0]);
// Rotate string in a way second character
// moves to the beginning.
rotate(str.begin(), str.begin() + 1, str.end());
}
}
// Driver code
int main()
{
string str;
cin>> str;
permute(str, "");
return 0;
}