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