Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package StacksAndQueues;
- import java.util.ArrayDeque;
- import java.util.Scanner;
- public class MatchingBrackets {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String expression = scanner.nextLine();
- ArrayDeque<Integer> stack = new ArrayDeque<>();
- for (int i = 0; i < expression.length(); i++) {
- char currentSymbol = expression.charAt(i);
- if (currentSymbol == '(') {
- stack.push(i);
- } else if (currentSymbol == ')') {
- int startSymbol = stack.pop();
- String contents = expression
- .substring(startSymbol, i + 1);
- System.out.println(contents);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment