0ms C solution with recursion

• The first time I wrote like:

``````char s[8000] = {NULL};
char temp[8000] = {NULL};
char* countAndSay(int n) {
``````

But I got a runtime error after I submitted it and I didn't know why.
So in my solution the declaration of variables `s` and `temp` is inside the fuction which may cause a waste of sapce.

``````char* countAndSay(int n) {
char * s = (char*) malloc(sizeof(char)*8000);
if(n<=4){
switch(n){
case 1: strcpy(s,"1"); break;
case 2: strcpy(s,"11"); break;
case 3: strcpy(s,"21"); break;
case 4: strcpy(s,"1211"); break;
}
return s;
}
else {
char* temp = countAndSay(n-1);
int len = strlen(temp), index = 0, i = 0;
while(i<len-2){
if(*(temp+i)==*(temp+i+1)){
if(*(temp+i+1)!=*(temp+i+2)){
s[index++] = '2';
s[index++] = *(temp+i);
i += 2;
}
else {
s[index++] = '3';
s[index++] = *(temp+i);
i += 3;
}
}
else {
s[index++] = '1';
s[index++] = *(temp+i);
i += 1;
}
}
if(i==len-1){
s[index++] = '1';
s[index] = *(temp+i);
}
else if(i==len-2){
if(*(temp+i)==*(temp+i+1)){
s[index++] = '2';
s[index] = *(temp+i);
}
else {
s[index++] = '1';
s[index++] = *(temp+(i++));
s[index++] = '1';
s[index] = *(temp+i);
}
}
return s;
}
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.