Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace T08._Balanced_Parenthesis
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- string input = Console.ReadLine();
- Stack<char> opening = new Stack<char>();
- for (int i = 0; i < input.Length; i++)
- {
- char currentChar = input[i];
- if (currentChar == '(' || currentChar == '[' || currentChar == '{')
- {
- opening.Push(currentChar);
- }
- else
- {
- if (opening.Count == 0)
- {
- Console.WriteLine("NO");
- Environment.Exit(0);
- }
- if (currentChar == ')')
- {
- if (opening.Pop() != '(')
- {
- Console.WriteLine("NO");
- Environment.Exit(0);
- }
- }
- else if (currentChar == '}')
- {
- if (opening.Pop() != '{')
- {
- Console.WriteLine("NO");
- Environment.Exit(0);
- }
- }
- else if (currentChar == ']')
- {
- if (opening.Pop() != '[')
- {
- Console.WriteLine("NO");
- Environment.Exit(0);
- }
- }
- }
- }
- Console.WriteLine("YES");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement