Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _08._Balanced_Parenthesis
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             char[] parenthes = Console.ReadLine().ToCharArray();
  12.  
  13.             Stack<char> stack = new Stack<char>(parenthes);
  14.             Queue<char> queue = new Queue<char>(parenthes);
  15.             char currentParenthesStack = ' ';
  16.             char currentParenthesQueue = ' ';
  17.             char mirrorParenthesStack = ' ';
  18.             char mirrorParenthesQueue = ' ';
  19.             char nextАttachedStack = ' ';
  20.             char nextАttachedQueue = ' ';
  21.  
  22.             if (parenthes.Length % 2 != 0 || parenthes.Length == 0)
  23.             {
  24.                 Console.WriteLine("NO");
  25.                 return;
  26.             }
  27.  
  28.             for (int i = 0; i < parenthes.Length / 2; i++)
  29.             {
  30.                 currentParenthesStack = stack.Pop();
  31.                 nextАttachedStack = stack.Peek();
  32.  
  33.                 currentParenthesQueue = queue.Dequeue();
  34.                 nextАttachedQueue = queue.Peek();
  35.  
  36.                 if (currentParenthesStack == '}')
  37.                 {
  38.                     mirrorParenthesStack = '{';
  39.                 }
  40.                 else if (currentParenthesStack == ')')
  41.                 {
  42.                     mirrorParenthesStack = '(';
  43.                 }
  44.                 else if (currentParenthesStack == ']')
  45.                 {
  46.                     mirrorParenthesStack = '[';
  47.                 }
  48.                 else if (currentParenthesStack == '{')
  49.                 {
  50.                     mirrorParenthesStack = '}';
  51.                 }
  52.                 else if (currentParenthesStack == '(')
  53.                 {
  54.                     mirrorParenthesStack = ')';
  55.                 }
  56.                 else if (currentParenthesStack == '[')
  57.                 {
  58.                     mirrorParenthesStack = ']';
  59.                 }
  60.  
  61.                 if (currentParenthesQueue == '}')
  62.                 {
  63.                     mirrorParenthesQueue = '{';
  64.                 }
  65.                 else if (currentParenthesQueue == ')')
  66.                 {
  67.                     mirrorParenthesQueue = '(';
  68.                 }
  69.                 else if (currentParenthesQueue == ']')
  70.                 {
  71.                     mirrorParenthesQueue = '[';
  72.                 }
  73.                 else if (currentParenthesQueue == '{')
  74.                 {
  75.                     mirrorParenthesQueue = '}';
  76.                 }
  77.                 else if (currentParenthesQueue == '(')
  78.                 {
  79.                     mirrorParenthesQueue = ')';
  80.                 }
  81.                 else if (currentParenthesQueue == '[')
  82.                 {
  83.                     mirrorParenthesQueue = ']';
  84.                 }
  85.  
  86.                 if (currentParenthesQueue == mirrorParenthesStack)// Exmp: ()
  87.                 {
  88.                     continue;
  89.                 }
  90.                 else
  91.                 {
  92.                     if (nextАttachedStack == mirrorParenthesStack && nextАttachedQueue == mirrorParenthesQueue)// Exmp: {}[]
  93.                     {
  94.                         stack.Pop();
  95.                         queue.Dequeue();
  96.                     }
  97.                     else
  98.                     {
  99.                         Console.WriteLine("NO");
  100.                         return;
  101.                     }
  102.                 }
  103.             }
  104.             Console.WriteLine("YES");
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement