You are viewing a single comment's thread. Return to all comments →
Scanner sc = new Scanner(System.in); Map map = new HashMap<>(); map.put('[', ']'); map.put('{', '}'); map.put('(', ')');
while (sc.hasNext()) { String input=sc.next(); Stack<Character> stack = new Stack<>(); char[] arr = input.toCharArray(); for (int i = 0; i < arr.length; i++) { if (!map.containsKey(arr[i])) { if (stack.isEmpty() || arr[i] != map.get(stack.peek())) { stack.push(arr[i]); break; } else { stack.pop(); } } else { stack.push(arr[i]); } } System.out.println(stack.isEmpty() ? "true" : "false"); }
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 →
Scanner sc = new Scanner(System.in); Map map = new HashMap<>(); map.put('[', ']'); map.put('{', '}'); map.put('(', ')');