Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.CodeDom;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _07BalancedParentheses
- {
- class Program
- {
- static void Main(string[] args)
- {
- var input = Console.ReadLine();
- Stack<char> stack = new Stack<char>();
- Queue<char> queue = new Queue<char>();
- char[] allowedChars = {'{', '[', '(', ')', ']', '}', ' '};
- bool areMirrored = true;
- foreach (char chr in input)
- {
- if (allowedChars.Contains(chr))
- {
- stack.Push(chr);
- queue.Enqueue(chr);
- }
- }
- if (stack.Count%2 == 1)
- {
- Console.WriteLine("NO");
- }
- else
- {
- int initialLenghtOfStack = stack.Count;
- while (stack.Count > initialLenghtOfStack/2)
- {
- char firstInStack = stack.Pop();
- char lastInQueue = queue.Dequeue();
- if (CompareParentheses(firstInStack, lastInQueue) == false)
- {
- areMirrored = false;
- }
- }
- if (areMirrored)
- {
- Console.WriteLine("YES");
- }
- else
- {
- Console.WriteLine("NO");
- }
- }
- }
- static bool CompareParentheses(char open, char closed)
- {
- if ((open - closed) == 2 || (open - closed) == 1 || (open == 32 && closed == 32))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement