Advertisement
Krassi_Daskalova

Matching Brackets

Jan 26th, 2022
929
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.ArrayDeque;
  2. import java.util.Scanner;
  3.  
  4. public class MatchingBrackets {
  5.     public static void main(String[] args){
  6.         Scanner scanner = new Scanner(System.in);
  7.         ArrayDeque<String> stack = new ArrayDeque<>();
  8.  
  9.         String input = scanner.nextLine();
  10.         for (int position = 0; position < input.length(); position++){
  11.             char current = input.charAt(position);
  12.             if(current == '('){
  13.                 stack.push(String.valueOf(position));
  14.             } else if(current == ')'){
  15.                 int start = Integer.parseInt(stack.pop());
  16.                 System.out.println(input.substring(start, position + 1));
  17.             }
  18.         }
  19.     }
  20. }
  21.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement