Advertisement
deyanmalinov

04. Matching Brackets

May 8th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.66 KB | None | 0 0
  1. package DPM;
  2.  
  3. import java.util.*;
  4.  
  5. public class Main {
  6.     public static void main(String[] args){
  7.         Scanner scan = new Scanner(System.in);
  8.         String line = scan.nextLine();
  9.         Deque<Integer> stack = new ArrayDeque<>();
  10.         for (int i = 0; i < line.length(); i++) {
  11.             char ch = line.charAt(i);
  12.  
  13.             if (ch == '(') {
  14.                 stack.push(i);
  15.             }
  16.             else if (ch == ')') {
  17.                 int startIndex = stack.pop();
  18.                 String contents =
  19.                         line.substring(startIndex, i + 1);
  20.                 System.out.println(contents);
  21.             }
  22.  
  23.         }
  24.         }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement