MiroslavKisov

MatchingBrackets

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