Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Balanced_Parenthesis
- {
- class BalancedParenthesis
- {
- static void Main(string[] args)
- {
- char[] inputText = Console.ReadLine().ToCharArray();
- Stack<char> brackets = new Stack<char>();
- bool isBalanced = true;
- foreach (char item in inputText)
- {
- bool isOpenBracket = ((item.Equals('{')) || (item.Equals('[')) || (item.Equals('(')));
- bool isCloseBracket = ((item.Equals('}')) || (item.Equals(']')) || (item.Equals(')')));
- if (isBalanced)
- {
- if (isOpenBracket)
- {
- brackets.Push(item);
- }
- else if ((isCloseBracket) && brackets.Count > 0)
- {
- if (((int)item == (int)(brackets.Peek() + 1)) || ((int)item == ((int)(brackets.Peek() + 2))))
- {
- brackets.Pop();
- }
- else
- {
- isBalanced = false;
- }
- }
- else
- {
- isBalanced = false;
- }
- }
- }
- if ((isBalanced) && brackets.Count.Equals(0))
- {
- Console.WriteLine("YES");
- }
- else
- {
- Console.WriteLine("NO");
- }
- }
- }
- }
Advertisement
RAW Paste Data
Copied
Advertisement