Advertisement
bullit3189

Balanced Brackets

Jan 25th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace SolvingExpression
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13.  
  14. int n = int.Parse(Console.ReadLine());
  15. int openedBracket = 0;
  16. int closedBracket = 0;
  17.  
  18. for (int i = 1; i <= n; i++)
  19. {
  20. string input = Console.ReadLine();
  21.  
  22. if (input == "(")
  23. {
  24. openedBracket++;
  25. if (openedBracket - closedBracket > 1)
  26. {
  27. Console.WriteLine("UNBALANCED");
  28. return;
  29. }
  30.  
  31. }
  32. else if (input == ")")
  33. {
  34. closedBracket++;
  35. if (openedBracket - closedBracket != 0)
  36. {
  37. Console.WriteLine("UNBALANCED");
  38. return;
  39. }
  40. }
  41. }
  42. if (openedBracket == closedBracket)
  43. {
  44. Console.WriteLine("BALANCED");
  45. }
  46. else
  47. {
  48. Console.WriteLine("UNBALANCED");
  49. }
  50.  
  51. }
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement