You are viewing a single comment's thread. Return to all comments →
in C:
char brackets[3][2] = { {'[', ']'}, {'(', ')'}, {'{', '}'}, }; char* isBalanced(char* s) { char* stack = calloc(sizeof(char), strlen(s)); int tos = 0; char b; while((b = *s)) { for(int i = 0; i < 3; i++) { if(brackets[i][0] == b) { stack[tos++] = b; } else if(brackets[i][1] == b) { char left_b = stack[--tos]; if(brackets[i][0] != left_b) { return "NO"; } } } ++s; } free(stack); return tos == 0 ? "YES" : "NO"; }
Seems like cookies are disabled on this browser, please enable them to open this website
Balanced Brackets
You are viewing a single comment's thread. Return to all comments →
in C: