We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Balanced Brackets
Balanced Brackets
Sort by
recency
|
1596 Discussions
|
Please Login in order to post a comment
Python
Thanks, everyone. I had the same problem with the code. I was starting to think that I was very bad at this.
Javascript
char* isBalanced(char* s) { char new_str[1001]; int top = -1; int len = strlen(s), flag = 0; for (int i = 0; i < len; i++){ top++; new_str[top] = s[i]; if (top >= 1 && is_a_pair(new_str[top-1], new_str[top])){ top -= 2; } if (top == -1 && i == len-1) flag = 1; } char res = malloc(4 sizeof(char)); if (flag) strcpy(res, "YES"); else strcpy(res, "NO"); return res; }
char* isBalanced(char* s) { char new_str[1001]; int top = -1; int len = strlen(s), flag = 0; for (int i = 0; i < len; i++){ top++; new_str[top] = s[i]; if (top >= 1 && is_a_pair(new_str[top-1], new_str[top])){ top -= 2; } if (top == -1 && i == len-1) flag = 1; } char res = malloc(4 sizeof(char)); if (flag) strcpy(res, "YES"); else strcpy(res, "NO"); return res; }