Advertisement
Guest User

Balanced Brackets

a guest
May 20th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.75 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 number = int.Parse(Console.ReadLine());
  10.             int countOpening = 0;
  11.             int countClosing = 0;
  12.             int secondOpeningCount = 0;
  13.             int secondClosingCount = 0;
  14.             string input = string.Empty;
  15.  
  16.             for (int i = 0; i < number; i++)
  17.             {
  18.                 input = Console.ReadLine();
  19.                 if (input != "(" && secondOpeningCount == 1)
  20.                     secondOpeningCount = 0;
  21.                 if (input != ")" && secondClosingCount == 1)
  22.                     secondClosingCount = 0;
  23.  
  24.                 if (input == "(")
  25.                 {
  26.                     countOpening++;
  27.                     secondOpeningCount++;
  28.                     if (secondOpeningCount == 2)
  29.                     {
  30.                         Console.WriteLine("UNBALANCED");
  31.                         return;
  32.                     }
  33.                 }
  34.                 else if (input == ")")
  35.                 {
  36.                     countClosing++;
  37.                     secondClosingCount++;
  38.                     if (secondOpeningCount == 2)
  39.                     {
  40.                         Console.WriteLine("UNBALANCED");
  41.                         return;
  42.                     }
  43.  
  44.                     if (countClosing == 1 && countOpening == 0)
  45.                     {
  46.                         Console.WriteLine("UNBALANCED");
  47.                         return;
  48.                     }
  49.                 }
  50.  
  51.             }
  52.  
  53.             if (countOpening == countClosing)
  54.                 Console.WriteLine("BALANCED");
  55.             else
  56.                 Console.WriteLine("UNBALANCED");
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement