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.
Use helper method to check if balanced then return boolean if the stack is empty
importjava.io.*;importjava.util.*;publicclassSolution{publicstaticvoidmain(String[]args){/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */Scannerinput=newScanner(System.in);while(input.hasNext()){Stringline=input.nextLine();Stack<Character>lineStak=newStack<>();for(charchars:line.toCharArray()){if(lineStak.empty()){lineStak.push(chars);}elseif(balanced(lineStak.peek(),chars))lineStak.pop();elselineStak.push(chars);}System.out.println(lineStak.empty());}}publicstaticbooleanbalanced(charc1,charc2){return(c1=='{'&&c2=='}')||(c1=='('&&c2==')')||(c1=='['&&c2==']');}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Stack
You are viewing a single comment's thread. Return to all comments →
Use helper method to check if balanced then return boolean if the stack is empty