Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _08._Balanced_Parenthesis
- {
- class Program
- {
- static void Main(string[] args)
- {
- char[] parenthes = Console.ReadLine().ToCharArray();
- Stack<char> stack = new Stack<char>(parenthes);
- Queue<char> queue = new Queue<char>(parenthes);
- char currentParenthesStack = ' ';
- char currentParenthesQueue = ' ';
- char mirrorParenthesStack = ' ';
- char mirrorParenthesQueue = ' ';
- char nextАttachedStack = ' ';
- char nextАttachedQueue = ' ';
- if (parenthes.Length % 2 != 0 || parenthes.Length == 0)
- {
- Console.WriteLine("NO");
- return;
- }
- for (int i = 0; i < parenthes.Length / 2; i++)
- {
- currentParenthesStack = stack.Pop();
- nextАttachedStack = stack.Peek();
- currentParenthesQueue = queue.Dequeue();
- nextАttachedQueue = queue.Peek();
- if (currentParenthesStack == '}')
- {
- mirrorParenthesStack = '{';
- }
- else if (currentParenthesStack == ')')
- {
- mirrorParenthesStack = '(';
- }
- else if (currentParenthesStack == ']')
- {
- mirrorParenthesStack = '[';
- }
- else if (currentParenthesStack == '{')
- {
- mirrorParenthesStack = '}';
- }
- else if (currentParenthesStack == '(')
- {
- mirrorParenthesStack = ')';
- }
- else if (currentParenthesStack == '[')
- {
- mirrorParenthesStack = ']';
- }
- if (currentParenthesQueue == '}')
- {
- mirrorParenthesQueue = '{';
- }
- else if (currentParenthesQueue == ')')
- {
- mirrorParenthesQueue = '(';
- }
- else if (currentParenthesQueue == ']')
- {
- mirrorParenthesQueue = '[';
- }
- else if (currentParenthesQueue == '{')
- {
- mirrorParenthesQueue = '}';
- }
- else if (currentParenthesQueue == '(')
- {
- mirrorParenthesQueue = ')';
- }
- else if (currentParenthesQueue == '[')
- {
- mirrorParenthesQueue = ']';
- }
- if (currentParenthesQueue == mirrorParenthesStack)// Exmp: ()
- {
- continue;
- }
- else
- {
- if (nextАttachedStack == mirrorParenthesStack && nextАttachedQueue == mirrorParenthesQueue)// Exmp: {}[]
- {
- stack.Pop();
- queue.Dequeue();
- }
- else
- {
- Console.WriteLine("NO");
- return;
- }
- }
- }
- Console.WriteLine("YES");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement