You are viewing a single comment's thread. Return to all comments →
import java.util.*; class Solution{
public static void main(String []argh) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String input=sc.next(); Stack<Character> s = new Stack<>(); Boolean flag=true; for (int i = 0; i < input.length(); i++) { if(flag) { char c = input.charAt(i); if(c=='(' || c=='{' || c=='['){s.push(c); } try{ if(c==')' && s.pop()!='('){flag=false; } if(c=='}' && s.pop()!='{'){flag=false; } if(c==']' && s.pop()!='['){flag=false; } }catch(Exception e){flag=false;} } } if(s.size()!=0){flag=false;} System.out.println(flag); } }
}
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 →
import java.util.*; class Solution{
}