Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace balanced_brackets
- {
- class Program
- {
- static void Main()
- {
- int numEntries = int.Parse(Console.ReadLine());
- int countOpen = 0;
- int countClose = 0;
- string previous = "";
- bool isBalanced = true;
- //string brackets = "";
- for (int i = 0; i < numEntries; i++)
- {
- string entry = Console.ReadLine();
- if (entry == "(")
- {
- if (previous == "(")
- {
- isBalanced = false;
- }
- countOpen++;
- previous = entry;
- }
- if (entry == ")")
- {
- if (previous == ")")
- {
- isBalanced = false;
- }
- countClose++;
- previous = entry;
- }
- }
- if (isBalanced && (countClose == countOpen) && previous == ")")
- Console.WriteLine("BALANCED");
- else
- Console.WriteLine("UNBALANCED");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment