Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace _08.BalancedParentheses
- {
- class Program
- {
- static void Main(string[] args)
- {
- string input = Console.ReadLine();
- Queue<char> firstPart = new Queue<char>();
- Stack<char> secondPart = new Stack<char>();
- if (input.Length % 2 == 0 && input.Length != 0)
- {
- for (int i = 0; i < input.Length / 2; i++)
- {
- firstPart.Enqueue(input[i]);
- }
- for (int i = input.Length / 2; i < input.Length; i++)
- {
- secondPart.Push(input[i]);
- }
- while (firstPart.Count > 0)
- {
- if (firstPart.Peek() == '{' && secondPart.Peek() == '}')
- {
- firstPart.Dequeue();
- secondPart.Pop();
- }
- else if (firstPart.Peek() == '(' && secondPart.Peek() == ')')
- {
- firstPart.Dequeue();
- secondPart.Pop();
- }
- else if (firstPart.Peek() == '[' && secondPart.Peek() == ']')
- {
- firstPart.Dequeue();
- secondPart.Pop();
- }
- else if (firstPart.Peek() == ' ' && secondPart.Peek() == ' ')
- {
- firstPart.Dequeue();
- secondPart.Pop();
- }
- else
- {
- Console.WriteLine("NO");
- return;
- }
- }
- Console.WriteLine("YES");
- }
- else
- {
- Console.WriteLine("NO");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment