forked from philona/cppcodes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutations.cpp
More file actions
64 lines (46 loc) · 1.51 KB
/
Permutations.cpp
File metadata and controls
64 lines (46 loc) · 1.51 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
#include <bits/stdc++.h>
using namespace std;
// Checking whether a map has all the values equal to 0 in its key-value mapping
bool isEmpty(map<char,int>m)
{
for(auto y=m.begin();y!=m.end();y++)
{
if(y->second==0)
continue;
else
return false;
}
return true;
}
void perm(map<char,int>freq,string r="")
{
// Checking if all the letters have been exhausted and no further Permutations can be formed
if(isEmpty(freq))
{
cout<<r;
return;
}
// Traversing through all the letters of the string and then Recursively Generating the Permutations
for(auto p=freq.begin();p!=freq.end();p++)
{
// If the Present Letter has already been exhausted, then do not add it and skip this specific iteration of the loop
if(p->second==0)
continue;
// Making a duplicate Map containing the frequency of all the letters as it is in the present map but the particular character,pointed in this specific Iteration of the Loop has Frequency less by 1 than found in the original map
map<char,int>temp;
for(auto y=freq.begin();y!=freq.end();y++)
temp[y->first]=y->second;
temp[p->first]--;
// Recursively calling the function with the newly formed map
perm(temp,r+p->first);
}
}
// Driver Code to test the Function
int main() {
string s="abaaa";
map<char,int>m;
for(char g:s)
m[g]++;
perm(m);
return 0;
}