forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
all_unique_permutation.cpp
57 lines (51 loc) · 1010 Bytes
/
all_unique_permutation.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
/*Problem Statement:
You are given a string containing numbers, the task is to print all the unique permutations
that are possible excluding the string that was entered by the user,that is the entered combination
of characters entered as string shouldn't be printed. */
#include<bits/stdc++.h>
using namespace std;
char temp[1000];
set<string>s;
void permute(string in, int i)
{
if(in[i]=='\0')
{
s.insert(in);
return;
}
for(int j=i;in[j]!='\0';j++)
{
swap(in[i],in[j]);
permute(in,i+1);
}
}
int main()
{
string in;
cout<<"Enter string: "<<endl;
cin>>in;
string temp;
temp=in;
permute(in,0);
set<string> :: iterator itr;
cout<<"all distinct permutations are: "<<endl;
for(itr=s.begin();itr!=s.end();itr++)
{
if(*itr>temp)
{
cout<<*itr<<endl;
}
}
return 0;
}
/*Example:
Input:-
Enter string:
112
Output:-
all distinct permutations are:
121
211
Time Complexity: O(n)
Space Complexity: O(n)
*/