|
| 1 | +#include <bits/stdc++.h> |
| 2 | +using namespace std; |
| 3 | + |
| 4 | +void printResult(char result[], int len) |
| 5 | +{ |
| 6 | + for (int i = 0; i < len; i++) |
| 7 | + printf("%c", result[i]); |
| 8 | + |
| 9 | + printf("\n"); |
| 10 | +} |
| 11 | + |
| 12 | +void Permutation(char c[], int count[], char result[], int depth, int len, int cs) |
| 13 | +{ |
| 14 | + if (depth == len) |
| 15 | + { |
| 16 | + printResult(result, len); |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + for (int i = 0; i < cs; i++) |
| 21 | + { |
| 22 | + if (count[i] != 0) |
| 23 | + { |
| 24 | + result[depth] = c[i]; |
| 25 | + count[i]--; |
| 26 | + Permutation(c, count, result, depth + 1, len, cs); |
| 27 | + count[i]++; |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +void StringPermutation(char exp[]) |
| 33 | +{ |
| 34 | + int l = strlen(exp); |
| 35 | + map<char, int> cmap; |
| 36 | + for (int i = 0; exp[i]; i++) |
| 37 | + { |
| 38 | + cmap.insert(pair<char, int>(exp[i], 0)); |
| 39 | + map<char, int> ::iterator it; |
| 40 | + |
| 41 | + it = cmap.find(exp[i]); |
| 42 | + ++it->second; |
| 43 | + |
| 44 | + cmap.insert(pair<char, int>(exp[i], it->second)); |
| 45 | + } |
| 46 | + char c[l + 1]; |
| 47 | + int oc[l + 1]; |
| 48 | + |
| 49 | + int cs = 0;//no of unique characters |
| 50 | + map<char, int>::iterator it; |
| 51 | + |
| 52 | + for (it = cmap.begin(); it != cmap.end(); it++) |
| 53 | + { |
| 54 | + c[cs] = it->first; |
| 55 | + oc[cs] = it->second; |
| 56 | + cs++; |
| 57 | + } |
| 58 | + |
| 59 | + char result[l + 1]; |
| 60 | + Permutation(c, oc, result, 0, l, cs); |
| 61 | +} |
| 62 | + |
| 63 | +int main() |
| 64 | +{ |
| 65 | + char exp[1000]; |
| 66 | + cout << "Enter expression : "; |
| 67 | + cin >> exp; |
| 68 | + |
| 69 | + StringPermutation(exp); |
| 70 | + return 0; |
| 71 | +} |
0 commit comments