Advertisement
kaloyanTry

BalancedBrackets

Sep 26th, 2020
1,488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.16 KB | None | 0 0
  1. using System;
  2.  
  3. namespace BalancedBrackets
  4. {
  5.     class Brackets
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int n = int.Parse(Console.ReadLine());
  10.  
  11.             int countOpenBrackets = 0;
  12.             int countClosedBrackets = 0;
  13.  
  14.             for (int i = 0; i < n; i++)
  15.             {
  16.                 string inputLine = Console.ReadLine();
  17.  
  18.                 char openBracket = '(';
  19.                 char closedBracket = ')';
  20.                
  21.  
  22.                 if (inputLine.Contains(openBracket))
  23.                 {
  24.                     countOpenBrackets++;
  25.                 }
  26.  
  27.                 if (inputLine.Contains(closedBracket))
  28.                 {
  29.                     countClosedBrackets++;
  30.                     if (countOpenBrackets - countClosedBrackets != 0)
  31.                     {
  32.                         break;
  33.                     }
  34.                 }
  35.             }
  36.            
  37.             if (countOpenBrackets == countClosedBrackets)
  38.             {
  39.                 Console.WriteLine("BALANCED");
  40.             }
  41.             else
  42.             {
  43.                 Console.WriteLine("UNBALANCED");
  44.             }
  45.         }
  46.     }
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement