MiroslavKisov

BalancedParentheses

Jan 24th, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace BalancedParentheses
  5. {
  6.     public class StartUp
  7.     {
  8.         public static void Main()
  9.         {
  10.             var input = Console.ReadLine().ToCharArray();
  11.             var parentheses = new Queue<char>(input);
  12.             var counter = 0;
  13.             char firstParent = ' ';
  14.             char secondParent = ' ';
  15.             bool isBalanced = true;
  16.             bool isFound = false;
  17.             if (parentheses.Count > 1 && parentheses.Count % 2 != 0)
  18.             {
  19.                 Console.WriteLine("NO");
  20.             }
  21.             else
  22.             {
  23.                 for (int i = 0; i < parentheses.Count; i++)
  24.                 {
  25.                     counter = 0;
  26.                     firstParent = parentheses.Peek();
  27.                     if (firstParent == '(' || firstParent == '[' || firstParent == '{')
  28.                     {
  29.                         isBalanced = CheckOpen(parentheses, ref counter, firstParent, secondParent);
  30.                         ResetPossition(parentheses, ref counter);
  31.                     }
  32.                     else if (firstParent == ')' || firstParent == ']' || firstParent == '}')
  33.                     {
  34.                         isBalanced = CheckClosed(parentheses, ref counter, firstParent, secondParent);
  35.                         ResetPossition(parentheses, ref counter);
  36.                     }
  37.                 }
  38.                 if (isBalanced)
  39.                 {
  40.                     Console.WriteLine("YES");
  41.                 }
  42.                 else
  43.                 {
  44.                     Console.WriteLine("NO");
  45.                 }
  46.             }
  47.         }
  48.  
  49.         private static void ResetPossition(Queue<char> parentheses, ref int counter)
  50.         {
  51.             for (int i = 0; i < counter; i++)
  52.             {
  53.                 parentheses.Enqueue(parentheses.Dequeue());
  54.             }
  55.         }
  56.  
  57.         private static bool CheckClosed(Queue<char> parentheses, ref int counter, char firstParent, char secondParent)
  58.         {
  59.             parentheses.Enqueue(parentheses.Dequeue());
  60.             for (int i = 0; i < parentheses.Count; i++)
  61.             {
  62.                 secondParent = parentheses.Peek();
  63.                 if (firstParent == ')' && secondParent == '(' && counter % 2 == 0)
  64.                 {
  65.                     return true;
  66.                 }
  67.                 else if (firstParent == '}' && secondParent == '{' && counter % 2 == 0)
  68.                 {
  69.                     return true;
  70.                 }
  71.                 else if (firstParent == ']' && secondParent == '[' && counter % 2 == 0)
  72.                 {
  73.                     return true;
  74.                 }
  75.                 else
  76.                 {
  77.                     parentheses.Enqueue(parentheses.Dequeue());
  78.                     counter++;
  79.                 }
  80.             }
  81.             return false;
  82.         }
  83.  
  84.         private static bool CheckOpen(Queue<char> parentheses, ref int counter, char firstParent, char secondParent)
  85.         {
  86.             parentheses.Enqueue(parentheses.Dequeue());
  87.             for (int i = 0; i < parentheses.Count; i++)
  88.             {
  89.                 secondParent = parentheses.Peek();
  90.                 if (firstParent == '(' && secondParent == ')' && counter % 2 == 0)
  91.                 {
  92.                     return true;
  93.                 }
  94.                 else if (firstParent == '{' && secondParent == '}' && counter % 2 == 0)
  95.                 {
  96.                     return true;
  97.                 }
  98.                 else if (firstParent == '[' && secondParent == ']' && counter % 2 == 0)
  99.                 {
  100.                     return true;
  101.                 }
  102.                 else
  103.                 {
  104.                     parentheses.Enqueue(parentheses.Dequeue());
  105.                     counter++;
  106.                 }
  107.             }
  108.             return false;
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment