You are viewing a single comment's thread. Return to all comments →
public static void main(String []argh) { Scanner sc = new Scanner(System.in);
while (sc.hasNext()) { String input=sc.next(); String[] values = input.split(""); Stack<String> stack = new Stack<>(); Map<String, String> chars = new HashMap<>(); chars.put("(", ")"); chars.put("{", "}"); chars.put("[", "]"); boolean malformed = false; for (String value: values){ if (chars.keySet().contains(value)){ stack.add(value); } else { try{ String openingChar = stack.pop(); if (!chars.get(openingChar).equals(value)){ malformed = true; } } catch (EmptyStackException e){ malformed = true; } } } System.out.println(stack.isEmpty() && !malformed ? "true" : "false"); //Complete the code } }
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 →
public static void main(String []argh) { Scanner sc = new Scanner(System.in);