Skip to content

Instantly share code, notes, and snippets.

@thevar1able
Created March 27, 2020 20:58
Show Gist options
  • Save thevar1able/da3416628d44087f1f03e8523ab76d6c to your computer and use it in GitHub Desktop.
Save thevar1able/da3416628d44087f1f03e8523ab76d6c to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#define OPEN_BRACKETS 5
#define STR_LENGTH 10
void gen(int length, int opened, int closed, char* str) {
if (opened + closed == 2 * length) {
printf("%s\n", str);
return;
}
if (opened < length) {
char new_str_a[STR_LENGTH];
strcpy(new_str_a, str);
strcat(new_str_a, "(");
gen(length, opened + 1, closed, new_str_a);
}
if (opened > closed) {
char new_str_b[STR_LENGTH];
strcpy(new_str_b, str);
strcat(new_str_b, ")");
gen(length, opened, closed + 1, new_str_b);
}
}
int main() {
char n[STR_LENGTH];
gen(OPEN_BRACKETS, 0, 0, n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment