I was solving the problem 20. Valid Parentheses in Leetcode using c and when they check this input s ="{[]}" the code returns false instead of true but I don't see any problem with the code:
typedef struct node{
char data;
struct node *next;
}node;
typedef node* stack;
int isEmpty(stack p){
return p ==NULL;
}
void push(stack *p,char x){
node *new = (node*)malloc(sizeof(node));
new->data = x;
new->next = *p;
*p = new;
}
void pop(stack *p){
node *t=*p;
*p=(*p)->next;
free(t);
}
bool isValid(char* s) {
stack p;
if(s[1] == '\0') return false;
for(int i=0;s[i]!= '\0';++i){
if(s[i]=='(' || s[i]=='[' || s[i]=='{'){
push(&p,s[i]);
}
else{
if((s[i]==')' && p->data == '(')||(s[i]==']' && p->data == '[') || (s[i]=='}' && p->data == '{'))
pop(&p);
else return false;
}
}
return isEmpty(p);
}
I really don't see any problem with the code, if you have any suggestion please help!