Advertisement
Guest User

Untitled

a guest
Aug 31st, 2019
510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. using System;
  2.  
  3. namespace balancedBrackets
  4. {
  5. class Program
  6. {
  7. static void Main()
  8. {
  9. int num = int.Parse(Console.ReadLine());
  10. int opening = 0;
  11. int closing = 0;
  12. bool beenOpened = false;
  13. bool balanced = true;
  14. for (int i = 0; i < num; i++)
  15. {
  16. string input = Console.ReadLine();
  17. if (beenOpened && input == "(")
  18. {
  19. Console.WriteLine("UNBALANCED");
  20. balanced = false;
  21. break;
  22. }
  23. else
  24. {
  25. beenOpened = false;
  26. }
  27. if (input == "(")
  28. {
  29. opening++;
  30. beenOpened = true;
  31. }
  32. else if (input == ")")
  33. {
  34. closing++;
  35. }
  36. if (input == "(" && i == num-1)
  37. {
  38. Console.WriteLine("UNBALANCED");
  39. balanced = false;
  40. break;
  41. }
  42.  
  43. }
  44. if (balanced)
  45. {
  46. if (opening == closing)
  47. {
  48. Console.WriteLine("BALANCED");
  49. }
  50. else
  51. {
  52. Console.WriteLine("UNBALANCED");
  53. }
  54. }
  55. }
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement