Prohause

Balanced Parenthesis

Sep 19th, 2018
690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace debbuger_new
  6. {
  7. public class StartUp
  8. {
  9. public static void Main(string[] args)
  10. {
  11. var input = Console.ReadLine();
  12.  
  13. var elements = new Stack<char>();
  14. var balanced = true;
  15.  
  16. if (input != null)
  17. foreach (var t in input)
  18. {
  19. switch (t)
  20. {
  21. case '(':
  22. elements.Push(t);
  23. break;
  24. case '[':
  25. elements.Push(t);
  26. break;
  27. case '{':
  28. elements.Push(t);
  29. break;
  30. case ')':
  31. if (!elements.Any() || elements.Pop() != '(')
  32. {
  33. balanced = false;
  34. }
  35. break;
  36. case ']':
  37. if (!elements.Any() || elements.Pop() != '[')
  38. {
  39. balanced = false;
  40. }
  41. break;
  42. case '}':
  43. if (!elements.Any() || elements.Pop() != '{')
  44. {
  45. balanced = false;
  46. }
  47. break;
  48. }
  49. }
  50.  
  51. Console.WriteLine(balanced ? "YES" : "NO");
  52. }
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment