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.
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");}}
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 →
My code looks exactly like your code but still I'm not able to pass 3 test cases...