Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace balancedBrackets
- {
- class Program
- {
- static void Main()
- {
- int num = int.Parse(Console.ReadLine());
- int opening = 0;
- int closing = 0;
- bool beenOpened = false;
- bool balanced = true;
- for (int i = 0; i < num; i++)
- {
- string input = Console.ReadLine();
- if (beenOpened && input == "(")
- {
- Console.WriteLine("UNBALANCED");
- balanced = false;
- break;
- }
- else
- {
- beenOpened = false;
- }
- if (input == "(")
- {
- opening++;
- beenOpened = true;
- }
- else if (input == ")")
- {
- closing++;
- }
- if (input == "(" && i == num-1)
- {
- Console.WriteLine("UNBALANCED");
- balanced = false;
- break;
- }
- }
- if (balanced)
- {
- if (opening == closing)
- {
- Console.WriteLine("BALANCED");
- }
- else
- {
- Console.WriteLine("UNBALANCED");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement