Advertisement
nsavov

Balanced_Brackets

May 27th, 2019
2,189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.26 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Balanced_Brackets
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int numberOfLines = int.Parse(Console.ReadLine());
  10.  
  11.             bool isOpened = false;
  12.             bool isBalanced = true;
  13.  
  14.             for(int n = 0; n < numberOfLines; n++)
  15.             {
  16.                 string input = Console.ReadLine();
  17.  
  18.                 if (input == "(")
  19.                 {
  20.                     if (!isOpened)
  21.                     {
  22.                         isOpened = true;
  23.                     }
  24.                     else
  25.                     {
  26.                         isBalanced = false;
  27.                     }
  28.                 }
  29.                
  30.                 if(input == ")")
  31.                 {
  32.                     if (isOpened)
  33.                     {
  34.                         isOpened = false;
  35.                     }
  36.                     else
  37.                     {
  38.                         isBalanced = false;
  39.                     }
  40.                 }
  41.             }
  42.  
  43.             if(isBalanced && !isOpened)
  44.             {
  45.                 Console.WriteLine("BALANCED");
  46.             }
  47.             else
  48.             {
  49.                 Console.WriteLine("UNBALANCED");
  50.             }
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement