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