forked from jpcsousa/codejam
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutation.cpp
More file actions
56 lines (46 loc) · 1.22 KB
/
Permutation.cpp
File metadata and controls
56 lines (46 loc) · 1.22 KB
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
#include <iostream>
#include <string.h>
using namespace std;
void DoPermute(char in[], char out[], int used[], int length, int recursLev)
{
//base case
if(recursLev == length) {
printf("%s\n", out);
return;
}
//Recursive Case
for(int i = 0; i < length; i++) {
if(used[i]) //If used, skip to next letter
continue;
out[recursLev] = in[i]; //Put current letter in output
used[i] = 1; //Mark this letter as used
DoPermute(in, out, used, length, recursLev + 1);
used[i] = 0; //Unmark this letter
}
}
int Permute(char inString[])
{
int length, *used;
char *out;
length = strlen(inString);
out = (char *)malloc(length + 1);
if(!out)
return 0; //Failed
//so printf doesn't run past the end of the buffer
out[length] = '\0';
used = (int *)malloc(sizeof(int) * length);
if(!used)
return 0; //Failed
//Start with no letters used, so zero array
for(int i = 0; i < length; i++)
used[i] = 0;
DoPermute(inString, out, used, length, 0);
free(out);
free(used);
return 1; //Success
}
int main()
{
Permute("abcdefghijklmnopqrstuvwxyz");
return 0;
}