Advertisement
Gabcho333

Untitled

Sep 15th, 2023
612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.72 KB | None | 0 0
  1. namespace T08._Balanced_Parenthesis
  2. {
  3.     internal class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             string input = Console.ReadLine();
  8.  
  9.             Stack<char> opening = new Stack<char>();
  10.  
  11.             for (int i = 0; i < input.Length; i++)
  12.             {
  13.                 char currentChar = input[i];
  14.  
  15.                 if (currentChar == '(' || currentChar == '[' || currentChar == '{')
  16.                 {
  17.                     opening.Push(currentChar);
  18.                 }
  19.  
  20.                 else
  21.                 {
  22.                     if (opening.Count == 0)
  23.                     {
  24.                         Console.WriteLine("NO");
  25.                         Environment.Exit(0);
  26.                     }
  27.  
  28.                     if (currentChar == ')')
  29.                     {
  30.                         if (opening.Pop() != '(')
  31.                         {
  32.                             Console.WriteLine("NO");
  33.                             Environment.Exit(0);
  34.                         }
  35.                     }
  36.                     else if (currentChar == '}')
  37.                     {
  38.                         if (opening.Pop() != '{')
  39.                         {
  40.                             Console.WriteLine("NO");
  41.                             Environment.Exit(0);
  42.                         }
  43.                     }
  44.                     else if (currentChar == ']')
  45.                     {
  46.                         if (opening.Pop() != '[')
  47.                         {
  48.                             Console.WriteLine("NO");
  49.                             Environment.Exit(0);
  50.                         }
  51.                     }
  52.                 }
  53.  
  54.             }
  55.  
  56.             Console.WriteLine("YES");
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement