ralichka

Untitled

Jan 17th, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace _08.BalancedParentheses
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             string input = Console.ReadLine();
  11.  
  12.             Queue<char> firstPart = new Queue<char>();
  13.             Stack<char> secondPart = new Stack<char>();
  14.  
  15.             if (input.Length % 2 == 0 && input.Length != 0)
  16.             {
  17.  
  18.                 for (int i = 0; i < input.Length / 2; i++)
  19.                 {
  20.                     firstPart.Enqueue(input[i]);
  21.                 }
  22.  
  23.                 for (int i = input.Length / 2; i < input.Length; i++)
  24.                 {
  25.                     secondPart.Push(input[i]);
  26.                 }
  27.  
  28.                 while (firstPart.Count > 0)
  29.                 {
  30.  
  31.                     if (firstPart.Peek() == '{' && secondPart.Peek() == '}')
  32.                     {
  33.                         firstPart.Dequeue();
  34.                         secondPart.Pop();
  35.                     }
  36.                     else if (firstPart.Peek() == '(' && secondPart.Peek() == ')')
  37.                     {
  38.                         firstPart.Dequeue();
  39.                         secondPart.Pop();
  40.                     }
  41.                     else if (firstPart.Peek() == '[' && secondPart.Peek() == ']')
  42.                     {
  43.                         firstPart.Dequeue();
  44.                         secondPart.Pop();
  45.                     }
  46.                     else if (firstPart.Peek() == ' ' && secondPart.Peek() == ' ')
  47.                     {
  48.                         firstPart.Dequeue();
  49.                         secondPart.Pop();
  50.                     }
  51.                     else
  52.                     {
  53.                         Console.WriteLine("NO");
  54.                         return;
  55.                     }
  56.                 }
  57.                 Console.WriteLine("YES");
  58.             }
  59.             else
  60.             {
  61.                 Console.WriteLine("NO");
  62.  
  63.             }
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment