struct Stack { char *val; int top, maxSize; }; void init(struct Stack *s, int maxSize) { s->val = malloc(maxSize); s->top = 0; } bool empty(struct Stack *s) { return !(s->top); } void push(struct Stack *s, char c) { s->val[s->top++] = c; } void pop(struct Stack *s) { s->top--; } char seek(struct Stack *s) { return s->val[s->top - 1]; } bool isValid(char *s) { int length = strlen(s); struct Stack stack; init(&stack, length); for (int i = 0; i < length; i++) { switch(s[i]) { case '(': case '{': case '[': push(&stack, s[i]); break; case ')': if (empty(&stack)) return false; if (seek(&stack) != '(') return false; pop(&stack); break; case '}': if (empty(&stack)) return false; if (seek(&stack) != '{') return false; pop(&stack); break; case ']': if (empty(&stack)) return false; if (seek(&stack) != '[') return false; pop(&stack); break; } } return empty(&stack); }