-
Notifications
You must be signed in to change notification settings - Fork 1
/
Permutation_of_string.cpp
60 lines (53 loc) · 1.45 KB
/
Permutation_of_string.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
53
54
55
56
57
58
59
60
//CODE FOR STRING LEXOGRAPHICAL PERMUTATION PRINTING (NON REPEATING)
// COPYRIGHT CONTENT BY: DEVANSH DUBEY
// BELOW CODE IS FOR HELP PURPOSE ONLY.
// RECOMMENDED TO CODE IT BY YOURSELF
// THANKS FOR YOUR VISIT :)
// FOLLOWING IS THE SOLUTION FOR "Creating Strings I" FROM CSES: REFER TO THE LINK -> https://cses.fi/problemset/task/1622
#include<bits/stdc++.h>
typedef unsigned long long ll;
#define N 1000000007
using namespace std;
ll fact(ll n){
ll sum = 1;
for(ll i = 1; i <= n; i++){
sum *= i;
}
return sum;
}
ll check(string s, int first, int prs){
for(int i = first; i < prs; i++)
if(s[i] == s[prs])
return 0;
return 1;
}
void permutation(string s, ll indx, ll n){
if(indx >= n){
cout << s << endl;
return;
}
for(ll i = indx; i < n; i++){
if(check(s, indx, i)){
swap(s[indx], s[i]);
permutation(s, indx+1, n);
swap(s[indx], s[i]);
}
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
string s;
cin >> s;
sort(s.begin(), s.end());
ll len = s.size(), freq[26] = {0}, i;
for(i = 0; i < len; i++)
freq[s[i] - 'a']++;
ll res = fact(len);
for(i = 0; i < len; i++){
if(freq[s[i] - 'a'] != 0){
res /= fact(freq[s[i] - 'a']); freq[s[i] - 'a'] = 0;}
}
cout << res << endl;
permutation(s, 0, len);
}