I am thinking the following algo will work. Please correct me if i am wrong.
void interleaveAndPrint(char * result, char * str1, char * str2, int i ) {
if (*str1 == '\0' && *str2 == '\0') {
result[i] = '\0';
printf("%s\n", result);
return;
}
if (*str1 == '\0'} {
// copy str2 to result
printf("%s", result);
return;
}
if (*str2 == '\0') {
// copy str1 to result
printf("%s", result);
return;
}
result[i] = *str1;
interleaveAndPrint(result, str1 + 1, str2, i + 1);
result[i] = *str2;
interleaveAndPrint(result, str1, str2 + 1, i + 1);
}
char * interleave(char * str1, char * str2) {
if (str1 == null || str2 == null) {
return str1 == null ? str2 : str1;
} else {
char * result = (char *) malloc(strlen(str1) + strlen(str2) + 1);
interleaveAndPrint(result, str1, str2, 0);
}
}
Thanks,
Immanuel
On Sun, Jul 24, 2011 at 8:55 AM, BL <[email protected]> wrote:
> Hi,
> Given two strings s and t of distinct characters print out all
> interleavings of the characters in the two strings using recursion.
> Does anybody know a suitable algorithm that could be used for this
> task?
>
> Thank you in advance.
> BL
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.