Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _15_Balanced_Brackets
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- string input = String.Empty;
- bool hasStart = false;
- bool balanced = false;
- int reminder = 0;
- for (int i = 1; i <= n; i++)
- {
- input = Console.ReadLine();
- try
- {
- char bracket = char.Parse(input);
- if (bracket == '(')
- {
- hasStart = true;
- balanced = true;
- }
- else if (hasStart && bracket == ')')
- {
- balanced = true;
- }
- else if (hasStart == false && bracket == ')')
- {
- balanced = false;
- break;
- }
- else if (hasStart && bracket == '(')
- {
- balanced = false;
- break;
- }
- }
- catch
- {
- }
- if (balanced == false)
- {
- reminder = i;
- break;
- }
- reminder=i;
- }
- if (reminder < n)
- {
- for (int i = reminder; i <= n; i++)
- {
- input = Console.ReadLine();
- }
- }
- if (balanced)
- {
- Console.WriteLine("BALANCED");
- }
- else
- {
- Console.WriteLine("UNBALANCED");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment