Advertisement
GeorgiGG

Untitled

Jul 1st, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.89 KB | None | 0 0
  1. using System;
  2. using System.CodeDom;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace _07BalancedParentheses
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             var input = Console.ReadLine();
  15.             Stack<char> stack = new Stack<char>();
  16.             Queue<char> queue = new Queue<char>();
  17.             char[] allowedChars = {'{', '[', '(', ')', ']', '}', ' '};
  18.             bool areMirrored = true;
  19.             foreach (char chr in input)
  20.             {
  21.                 if (allowedChars.Contains(chr))
  22.                 {
  23.                     stack.Push(chr);
  24.                     queue.Enqueue(chr);
  25.                 }
  26.             }
  27.             if (stack.Count%2 == 1)
  28.             {
  29.                 Console.WriteLine("NO");
  30.             }
  31.             else
  32.             {
  33.                 int initialLenghtOfStack = stack.Count;
  34.                 while (stack.Count > initialLenghtOfStack/2)
  35.                 {
  36.                     char firstInStack = stack.Pop();
  37.                     char lastInQueue = queue.Dequeue();
  38.                     if (CompareParentheses(firstInStack, lastInQueue) == false)
  39.                     {
  40.                         areMirrored = false;
  41.                     }
  42.                 }
  43.                 if (areMirrored)
  44.                 {
  45.                     Console.WriteLine("YES");
  46.                 }
  47.                 else
  48.                 {
  49.                     Console.WriteLine("NO");
  50.                 }
  51.             }
  52.         }              
  53.         static bool CompareParentheses(char open, char closed)
  54.         {
  55.             if ((open - closed) == 2 || (open - closed) == 1 || (open == 32 && closed == 32))
  56.             {
  57.                 return true;
  58.             }
  59.             else
  60.             {
  61.                 return false;
  62.             }
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement