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); List<String> input = new ArrayList<>(); while (sc.hasNext()) { input.add(sc.next()); } List<Stack<Character>> stacks = new ArrayList<>(); for(String st : input) { Stack<Character> characters = new Stack<>(); for (char ch : st.toCharArray()) { characters.push(ch); } stacks.add(characters); } for(Stack stk : stacks) { int a = 0; int b = 0; int c = 0; for(int i = stk.size() -1; i > -1 ; i--) { Character ch = (Character) stk.elementAt(i); if((ch == '(' )) { stk.pop(); a++; } if((ch == ')' )) { stk.pop(); if(a == 1){ a = 3; } a--; } if((ch == '[' )) { stk.pop(); if(b == 1){ a = 3; } b++; } if((ch == ']' )) { stk.pop(); if(b == 1){ b = 3; } b--; } if((ch == '{' )) { stk.pop(); c++; } if((ch == '}' )) { stk.pop(); if(c == 1){ c = 3; } c--; } } System.out.println(((a+b+c) == 0)? true : false); } }
}
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Java Stack
You are viewing a single comment's thread. Return to all comments →
import java.util.*; class Solution{
}