Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. namespace Balanced_Parentheses
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5.  
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. { //{[()()()]} - check for that case!!!!
  10. string line = Console.ReadLine();
  11.  
  12. if (line.Length % 2 != 0)
  13. {
  14. Console.WriteLine("NO");
  15. return;
  16. }
  17.  
  18. Stack<char> stack = new Stack<char>();
  19.  
  20. bool isBalanced = true;
  21.  
  22. foreach (var ch in line)
  23. {
  24. if (ch == '{' || ch == '[' || ch == '(')
  25. {
  26. stack.Push(ch);
  27. }
  28. else if (ch == '}')
  29. {
  30. if (stack.Pop() != '{')
  31. {
  32. isBalanced = false;
  33. break;
  34. }
  35. }
  36. else if (ch == ']')
  37. {
  38. if (stack.Pop() != '[')
  39. {
  40. isBalanced = false;
  41. break;
  42. }
  43. }
  44. else if (ch == ')')
  45. {
  46. if (stack.Pop() != '(')
  47. {
  48. isBalanced = false;
  49. break;
  50. }
  51. }
  52. }
  53.  
  54. Console.WriteLine(isBalanced ? "YES" : "NO");
  55. }
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement