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(); //Complete the code Stack<Character> stack = new Stack<>(); char[] ch = input.toCharArray(); for (int i = 0; i < ch.length; i++) { if (!stack.isEmpty() && ((ch[i] == ')' && stack.peek() == '(') || (ch[i] == ']' && stack.peek() == '[') || (ch[i] == '}' && stack.peek() == '{')) ) { stack.pop(); } else { stack.push(ch[i]); } } System.out.println(stack.isEmpty() ? true : false); } 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 →
import java.util.*; class Solution{
}