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.
Yes it is. If !s.empty() is placed first and the stack is empty, s.peek()=='[' will not be executed, hence no runtime error.This is due to the short circuit evaluation nature of if statements. However the order you gave above gives runtime error because s.peek()=='[' is executed first and this may cause errors if the stack is empty.
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 →
Yes it is. If !s.empty() is placed first and the stack is empty, s.peek()=='[' will not be executed, hence no runtime error.This is due to the short circuit evaluation nature of if statements. However the order you gave above gives runtime error because s.peek()=='[' is executed first and this may cause errors if the stack is empty.