-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathStringPermutations.cpp
71 lines (59 loc) · 1.14 KB
/
StringPermutations.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
61
62
63
64
65
66
67
68
69
70
71
#include <bits/stdc++.h>
using namespace std;
void printResult(char result[], int len)
{
for (int i = 0; i < len; i++)
printf("%c", result[i]);
printf("\n");
}
void Permutation(char c[], int count[], char result[], int depth, int len, int cs)
{
if (depth == len)
{
printResult(result, len);
return;
}
for (int i = 0; i < cs; i++)
{
if (count[i] != 0)
{
result[depth] = c[i];
count[i]--;
Permutation(c, count, result, depth + 1, len, cs);
count[i]++;
}
}
}
void StringPermutation(char exp[])
{
int l = strlen(exp);
map<char, int> cmap;
for (int i = 0; exp[i]; i++)
{
cmap.insert(pair<char, int>(exp[i], 0));
map<char, int> ::iterator it;
it = cmap.find(exp[i]);
++it->second;
cmap.insert(pair<char, int>(exp[i], it->second));
}
char c[l + 1];
int oc[l + 1];
int cs = 0;//no of unique characters
map<char, int>::iterator it;
for (it = cmap.begin(); it != cmap.end(); it++)
{
c[cs] = it->first;
oc[cs] = it->second;
cs++;
}
char result[l + 1];
Permutation(c, oc, result, 0, l, cs);
}
int main()
{
char exp[1000];
cout << "Enter expression : ";
cin >> exp;
StringPermutation(exp);
return 0;
}