You are viewing a single comment's thread. Return to all comments →
Java 15+ (using my own Stack class, I forgot that already exists this class :P)
import java.util.*; class Stack { private List<Character> elements; public Stack() { this.elements = new ArrayList<Character>(); } public Stack(List<Character> elements) { this.elements = new ArrayList<>(elements); } public int size() { return this.elements.size(); } public void push(Character element) { this.elements.add(element); } public Character pop() { if (isEmpty()) return null; var element = elements.get(size() - 1); elements.remove(size() - 1); return element; } public boolean isEmpty() { return size() == 0; } } public class Solution { public static void main(String[] args) { var matches = Map.of('{', '}', '[', ']', '(', ')'); var openElems = matches.keySet(); try (var scanner = new Scanner(System.in)) { while(scanner.hasNext()) { var line = scanner.nextLine(); var closeStack = new Stack(); if (line.length() % 2 != 0) { System.out.println(false); continue; } var result = true; var index = line.length() - 1; while (index >= 0) { var c = line.charAt(index); if (openElems.contains(c)) { var closeElem = closeStack.pop(); if (matches.get(c) != closeElem) { result = false; break; } } else closeStack.push(c); index--; } if (closeStack.size() > 0) result = false; System.out.println(result); } } } }
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+ (using my own Stack class, I forgot that already exists this class :P)