-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPallindrome Partioning
71 lines (59 loc) · 1.61 KB
/
Pallindrome Partioning
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
70
71
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
char*** ans;
int ansTop;
char** path;
int pathTop;
int* length;
bool isPalindrome(char* s, int start, int end){
for(int i=start, j=end; i<j; i++,j--){
if(s[i]!=s[j]){
return false;
}
}
return true;
}
char* cutString(char* s, int start, int end){
char* tmp=malloc(sizeof(char)*(end-start+2));
int index=0;
for(int i=start; i<=end; i++){
tmp[index++]=s[i];
}
tmp[index]='\0';
return tmp;
}
void backTracking(char* s, int startIndex){
if(startIndex >= strlen(s)){
char** tmp=malloc(sizeof(char*)*pathTop);
for(int i=0; i<pathTop; i++){
tmp[i]=path[i];
}
ans[ansTop]=tmp;
length[ansTop++]=pathTop;
}
for(int i=startIndex; i<strlen(s); i++){
if(isPalindrome(s, startIndex, i)){
path[pathTop++]=cutString(s, startIndex, i);
}else{
continue;
}
backTracking(s, i+1);
pathTop--;
}
}
char *** partition(char * s, int* returnSize, int** returnColumnSizes){
ans=malloc(sizeof(char**)*40000);
path=malloc(sizeof(char*)*strlen(s));
length=malloc(sizeof(int)*40000);
ansTop=pathTop=0;
backTracking(s, 0);
*returnSize=ansTop;
*returnColumnSizes=malloc(sizeof(int)*ansTop);
for(int i=0; i<ansTop; i++){
(*returnColumnSizes)[i]=length[i];
}
return ans;
}