Not able to pass last test case when i get the close bracket as a input when stack is empty i am not able to build this logic

hello @anshulgurawalia

 while( ch[i]!='\0'){  
   if( ch[i]=='(' or ch[i]=='{' or ch[i]=='['){ 
    s.push( ch[i] );
   }  
   else {  
      if( !s.empty( )){ 
         s.pop( );   
      }  
   } 
   i++;  
 } 

here in else part u need check following thing
a) if stack is empty then return false
b) if stack is not empty then top of the stack must match with opposite bracket of ch[i]

for eg we are checking for this :- ( ( ) ) ) ( )

  1. ( <-push into stack
  2. ( <- push into stack
  3. ) <- pop out the top
  4. ) <- pop out the top
    now our stack will become empty
  5. ) <- NOW, we return false or not ???
    can you please drive run the code on above mentioned string from step 5

returrn false from here only , becuase the stack is empty …

but the loop will cont. move till we get the null operator

no , return statement stop the function exection and control will return back to main

check ur updated code here->

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.