-
Notifications
You must be signed in to change notification settings - Fork 2
/
question1.c
69 lines (52 loc) · 1.03 KB
/
question1.c
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
/*
Generate all permutations of a given string
Backtracking is a concept wherein at each point we backtrack to see what was done and was is left
METHOD:
Idea is to keep swapping at each level to achieve this
*/
// C program to print all permutations with duplicates allowed
#include <stdio.h>
#include <string.h>
#include <string.h>
int hash[256];
void initHash(){
int i;
for(i=0;i<256;i++){
hash[i] = 0;
}
}
void outputResult(char *result){
int i, length = strlen(result);
for(i=0;i<length;i++){
printf("%c ", result[i]);
}
printf("\n");
}
void permute(char *str, int size, char *result, int level){
if(level == size){
outputResult(result);
return;
}
int i;
for(i=0; i<size; i++){
if(hash[str[i]] == 0){
continue;
}
result[level] = str[i];
hash[str[i]]--;
permute(str, size, result, level + 1);
hash[str[i]]++;
}
}
int main(){
char str[] = "ABC";
int length = strlen(str);
char result[length];
int i;
initHash();
for(i=0;i<length;i++){
hash[str[i]]++;
}
permute(str,length,result,0);
return 0;
}