Advertisement
Guest User

Balanced_Parentheses

a guest
Jan 21st, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 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 _08_Balanced_Parentheses
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. var input = Console.ReadLine();
  14. var stack = new Stack<char>();
  15. bool check = true;
  16.  
  17. for (int i = 0; i < input.Length; i++)
  18. {
  19. var symbol = input[i];
  20.  
  21. if ((symbol == '{') || (symbol == '[') || (symbol == '('))
  22.  
  23. {
  24. stack.Push(symbol);
  25.  
  26. }
  27.  
  28. if (symbol == '}')
  29. {
  30. if (stack.Any())
  31. {
  32.  
  33.  
  34. if (stack.Peek() == '{')
  35. {
  36. stack.Pop();
  37. check = true;
  38. }
  39.  
  40. else if (symbol != '{')
  41. {
  42. check = false;
  43. break;
  44. }
  45. }
  46. }
  47.  
  48. if (symbol == ')')
  49. {
  50. if (stack.Any())
  51. {
  52.  
  53.  
  54. if (stack.Peek() == '(')
  55. {
  56. stack.Pop();
  57. check = true;
  58. }
  59.  
  60. else if (symbol != '(')
  61. {
  62. check = false;
  63.  
  64. }
  65. }
  66. }
  67.  
  68. if (symbol == ']')
  69. {
  70. if (stack.Any())
  71. {
  72.  
  73. if (stack.Peek() == '[')
  74. {
  75. stack.Pop();
  76. check = true;
  77. }
  78.  
  79. else if (symbol != '[')
  80. {
  81. check = false;
  82. break;
  83. }
  84. }
  85.  
  86.  
  87. }
  88. }
  89.  
  90.  
  91. if (check == true)
  92. {
  93. Console.WriteLine("YES");
  94. }
  95. else
  96. {
  97. Console.WriteLine("NO");
  98. }
  99. }
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement