Advertisement
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 balanced = false;
- StringBuilder sb = new StringBuilder(input);
- for (int i = 1; i <= n; i++)
- {
- sb.Append(Console.ReadLine());
- }
- string brackets = String.Empty;
- StringBuilder sb2 = new StringBuilder(brackets);
- for (int i = 0; i < sb.Length; i++)
- {
- if (sb[i] == '(' || sb[i] == ')')
- {
- sb2.Append(sb[i]);
- }
- }
- for (int i = 1; i <= sb2.Length; i+=2)
- {
- if (sb2[0]==')' || sb2[sb2.Length-1] == '(')
- {
- balanced = false;
- break;
- }
- if (sb2[i - 1] == '(' && sb2[i] == ')')
- {
- balanced = true;
- }
- else
- {
- balanced = false;
- break;
- }
- }
- if (balanced)
- {
- Console.WriteLine("BALANCED");
- }
- else
- {
- Console.WriteLine("UNBALANCED");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement