Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace P08BalancedParantheses
- {
- class Program
- {
- static void Main(string[] args)
- {
- string input = Console.ReadLine();
- // bool isBalanced = true;
- var stack = new Stack<char>();
- if(input.Length == 0)
- {
- Console.WriteLine("NO");
- return;
- }
- else if (input.Length % 2 != 0)
- {
- Console.WriteLine("NO");
- return;
- }
- for (int i = 0; i < input.Length / 2; i++)
- {
- stack.Push(input[i]);
- }
- // Console.WriteLine(string.Join(" ", stack));
- for (int j = 0; j < input.Length / 2; j++)
- {
- char symbolToCheck = stack.Peek();
- if ((symbolToCheck == '[' && input[j + input.Length / 2] == ']')
- || (symbolToCheck == '{' && input[j + input.Length / 2] == '}')
- || (symbolToCheck == '(' && input[j + input.Length / 2] == ')'))
- {
- stack.Pop();
- }
- else
- {
- Console.WriteLine("NO");
- break;
- }
- }
- if (stack.Count == 0)
- {
- Console.WriteLine("YES");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement