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(); Deque <Character> q = new ArrayDeque<>(); Deque <Character> q1 = new ArrayDeque<>(); char [] arr = input.toCharArray(); for (char c: arr){ q.push(c); } boolean p = false; while (!q.isEmpty()){ char c = q.pop(); if (c=='}' || c==')' || c==']' ){ q1.push(c); } if ((c=='{' || c=='(' || c=='[')){ if (q1.isEmpty()){ p=false; break; } if (c=='{' && q1.pop()=='}') p=true; else if (c=='(' && q1.pop()==')') p=true; else if (c=='[' && q1.pop()==']') p=true; else{ p=false; break; } } } if (!q1.isEmpty()){ p=false; } System.out.println(p); //sc.close(); } } }
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 →