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.
Please reply ..
If we write the statement on either side of && as the order shown above ( peek before and empty after) , it is showing runtime error .
Is the order of statement also important in writing these expressions ?
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.
My code looks exactly like your code but still I'm not able to pass 3 test cases...
importjava.util.Scanner;importjava.util.Stack;/** * Created by BK on 06-08-2017. */publicclassBalancedParanthesis{publicstaticvoidmain(String...strings){Scannersc=newScanner(System.in);inttc=sc.nextInt();for(inti=0;i<tc;i++){printAnswer(sc.next());}}privatestaticvoidprintAnswer(Stringinput){Stack<Character>stack=newStack<>();booleanisValid=true;for(inti=0;i<input.length();i++){charcurrentChar=input.charAt(i);if(currentChar=='{'||currentChar=='('||currentChar=='[')stack.push(currentChar);elseif(currentChar=='}'){if(stack.isEmpty())isValid=false;else{if(stack.pop()!='{'){isValid=false;}}}elseif(currentChar==')'){if(stack.isEmpty())isValid=false;else{if(stack.pop()!='('){isValid=false;}}}elseif(currentChar==']'){if(stack.isEmpty())isValid=false;else{if(stack.pop()!='['){isValid=false;}}}}System.out.println(isValid?"YES":"NO");}}
if you put one more check i.e,
**if (!(s.length()%2 ==0) ) **
before for loop it will improve it's performance and many cases straight forward it will be passed
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
Balanced Brackets
You are viewing a single comment's thread. Return to all comments →
My Java Solution
Please reply .. If we write the statement on either side of && as the order shown above ( peek before and empty after) , it is showing runtime error . Is the order of statement also important in writing these expressions ?
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.
Another questionisthat why !s.empty() is even required in the ?
I think You can't pop is stack is empty
If stack is not empty... then there is an extra unbalanced parenthesis on the stack...
My code looks exactly like your code but still I'm not able to pass 3 test cases...
import java.util.Scanner; import java.util.Stack;
public class Solution {
}
one query: If stack is empty then peek should return null. Is there any need for this check "!stack.empty() "
if you put one more check i.e, **if (!(s.length()%2 ==0) ) ** before for loop it will improve it's performance and many cases straight forward it will be passed