Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Balanced_Brackets
- {
- class Program
- {
- static void Main(string[] args)
- {
- int number = int.Parse(Console.ReadLine());
- int countOpening = 0;
- int countClosing = 0;
- int secondOpeningCount = 0;
- int secondClosingCount = 0;
- string input = string.Empty;
- for (int i = 0; i < number; i++)
- {
- input = Console.ReadLine();
- if (input != "(" && secondOpeningCount == 1)
- secondOpeningCount = 0;
- if (input != ")" && secondClosingCount == 1)
- secondClosingCount = 0;
- if (input == "(")
- {
- countOpening++;
- secondOpeningCount++;
- if (secondOpeningCount == 2)
- {
- Console.WriteLine("UNBALANCED");
- return;
- }
- }
- else if (input == ")")
- {
- countClosing++;
- secondClosingCount++;
- if (secondOpeningCount == 2)
- {
- Console.WriteLine("UNBALANCED");
- return;
- }
- if (countClosing == 1 && countOpening == 0)
- {
- Console.WriteLine("UNBALANCED");
- return;
- }
- }
- }
- if (countOpening == countClosing)
- Console.WriteLine("BALANCED");
- else
- Console.WriteLine("UNBALANCED");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement