You are viewing a single comment's thread. Return to all comments →
Java 15, working:
Map<String, String> openCloses = Map.of( "{", "}", "[","]", "(", ")"); Scanner in = new Scanner(System.in); while (in.hasNext()) { Stack<String> stack = new Stack<>(); boolean isBalanced = true; for (String string : in.nextLine().split("")) { if (openCloses.containsKey(string)) { stack.push(string); } else if (!stack.empty() && openCloses.containsValue(string)) { String opener = stack.pop(); boolean isClosing = openCloses.get(opener).equals(string); isBalanced = isBalanced && isClosing; } else { isBalanced = false; } } isBalanced = isBalanced && stack.empty(); System.out.println(isBalanced); }
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 15, working: