Advertisement
Dianov

Data Types and Variables - More Exercise (06. Balanced Brackets)

Aug 7th, 2021
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.39 KB | None | 0 0
  1. using System;
  2.  
  3. namespace BalancedBrackets
  4. {
  5.     class Program
  6.     {
  7.         public static int J { get; private set; }
  8.  
  9.         static void Main(string[] args)
  10.         {
  11.             int rowsNumber = int.Parse(Console.ReadLine());
  12.             int openingBracketsCounter = 0;
  13.             int closingBracketsCounter = 0;
  14.  
  15.             for (int i = 0; i < rowsNumber; i++)
  16.             {
  17.                 string userInput = Console.ReadLine();
  18.                 for (int j = 0; j < userInput.Length; j++)
  19.                 {
  20.                     if (userInput[j] == '(' && openingBracketsCounter == closingBracketsCounter)
  21.                     {
  22.                         openingBracketsCounter++;
  23.                     }
  24.                     else if (userInput[j] == '(' && openingBracketsCounter > closingBracketsCounter)
  25.                     {
  26.                         Console.WriteLine("UNBALANCED");
  27.                         Environment.Exit(0);
  28.                     }
  29.                     else if (userInput[j] == ')')
  30.                     {
  31.                         closingBracketsCounter++;
  32.                     }
  33.                 }
  34.             }
  35.  
  36.             if (openingBracketsCounter == closingBracketsCounter)
  37.             {
  38.                 Console.WriteLine("BALANCED");
  39.             }
  40.             else
  41.             {
  42.                 Console.WriteLine("UNBALANCED");
  43.             }
  44.         }
  45.     }
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement