Advertisement
bullit3189

Balanced Brackets - Second option

Jan 25th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 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 _15_Balanced_Brackets
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. int n = int.Parse(Console.ReadLine());
  14. string input = String.Empty;
  15.  
  16. bool balanced = false;
  17.  
  18. StringBuilder sb = new StringBuilder(input);
  19.  
  20. for (int i = 1; i <= n; i++)
  21. {
  22. sb.Append(Console.ReadLine());
  23. }
  24. string brackets = String.Empty;
  25. StringBuilder sb2 = new StringBuilder(brackets);
  26. for (int i = 0; i < sb.Length; i++)
  27. {
  28. if (sb[i] == '(' || sb[i] == ')')
  29. {
  30. sb2.Append(sb[i]);
  31. }
  32. }
  33.  
  34. for (int i = 1; i <= sb2.Length; i+=2)
  35. {
  36. if (sb2[0]==')' || sb2[sb2.Length-1] == '(')
  37. {
  38. balanced = false;
  39. break;
  40. }
  41. if (sb2[i - 1] == '(' && sb2[i] == ')')
  42. {
  43. balanced = true;
  44. }
  45. else
  46. {
  47. balanced = false;
  48. break;
  49. }
  50. }
  51.  
  52.  
  53. if (balanced)
  54. {
  55. Console.WriteLine("BALANCED");
  56. }
  57. else
  58. {
  59. Console.WriteLine("UNBALANCED");
  60. }
  61. }
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement