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.
@benedict_ I think if string is "))" or any string starts with closing brackets your code will fail.
try this,it might help you
#include<math.h>#include<stdio.h>#include<string.h>#include<stdlib.h>#include<assert.h>#include<limits.h>#include<stdbool.h>structStack{intdata;structStack*next;};typedefstructStacks;s*top=NULL,*neu;voidpush(intdata){neu=(s*)malloc(sizeof(s));neu->data=data;neu->next=NULL;if(!top){top=neu;}else{neu->next=top;top=neu;}}voidpop(){s*temp=top;top=top->next;free(temp);}char*isBalanced(char*s){// Complete this functioninti,len,flag=1;len=strlen(s);for(i=0;i<len;i++){if((s[i]==')'||s[i]=='}'||s[i]==']')&&(!top)){push(7);break;}else{if(s[i]=='('){push(1);}if(s[i]=='{'){push(2);}if(s[i]=='['){push(3);}if(s[i]==')'&&top->data==1){pop();}if(s[i]=='}'&&top->data==2){pop();}if(s[i]==']'&&top->data==3){pop();}}}if(top){while(top){structStack*temp;temp=top;top=top->next;free(temp);}return"NO";}else{return"YES";}}intmain(){intt;scanf("%i",&t);for(inta0=0;a0<t;a0++){char*s=(char*)malloc(512000*sizeof(char));scanf("%s",s);intresult_size;char*result=isBalanced(s);printf("%s\n",result);}return0;}
Cookie support is required to access HackerRank
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 →
@benedict_ I think if string is "))" or any string starts with closing brackets your code will fail.
try this,it might help you