yanass

Balanced Brackets

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