-
Notifications
You must be signed in to change notification settings - Fork 77
/
ReturnPermutationsString.cpp
60 lines (49 loc) · 1.04 KB
/
ReturnPermutationsString.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
// Given a string S, find and return all the possible permutations of the input string.
// Input Format :
// String S
// Output Format :
// All permutations (in different lines)
// Input :
// abc
// Output :
// abc
// acb
// bac
// bca
// cab
// cba
#include <iostream>
#include <string>
#include <string>
using namespace std;
int returnPermutations(string input, string output[]){
if(input.length()==0)
{
output[0]="";
return 1;
}
string result[1000];
int x,length=0;
char ch;
for(int i=0; i<input.length(); i++)
{
ch=input[i];
string restOfString=input.substr(0,i)+input.substr(i+1);
x=returnPermutations(restOfString,result);
for(int j=0; j<x; j++)
{
output[length++]=ch+result[j];
}
}
return length;
}
int main(){
string input;
cin >> input;
string output[10000];
int count = returnPermutations(input, output);
for(int i = 0; i < count && i < 10000; i++){
cout << output[i] << endl;
}
return 0;
}