gabi11

Stacks and Queues - 4. Matching Brackets

May 10th, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.75 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. namespace Advanced
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             var input = Console.ReadLine();
  13.             var stack = new Stack<int>();
  14.  
  15.             for (int i = 0; i < input.Length; i++)
  16.             {
  17.                 if (input[i] == '(')
  18.                 {
  19.                     stack.Push(i);
  20.                 }
  21.  
  22.                 if (input[i] == ')')
  23.                 {
  24.                     var startIndex = stack.Pop();
  25.                     string result = input.Substring(startIndex, i - startIndex + 1);
  26.                     Console.WriteLine(result);
  27.                 }
  28.             }
  29.         }
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment