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.
In C language, not using a stack but mimicking it:
char*isBalanced(char*s){char*s2=malloc(strlen(s)*sizeof(char));memset(s2,strlen(s),sizeof(char));// Not mandatory. To keep memory cleanchar*p1=s;char*p2=s2;boolisBalanced=true;while(isBalanced&&*p1){switch(*p1){case'{':case'(':case'[':*(p2++)=*p1;break;case'}':if(p2>s2&&*(p2-1)=='{')*(--p2)='\0';elseisBalanced=false;break;case')':if(p2>s2&&*(p2-1)=='(')*(--p2)='\0';elseisBalanced=false;break;case']':if(p2>s2&&*(p2-1)=='[')*(--p2)='\0';elseisBalanced=false;break;}p1++;}if(*p1=='\0'&&p2>s2)isBalanced=false;free(s2);char*response=malloc(4*sizeof(char));if(isBalanced)strcpy(response,"YES");elsestrcpy(response,"NO");returnresponse;}
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 →
In C language, not using a stack but mimicking it: