Advertisement
Guest User

Untitled

a guest
Apr 15th, 2020
1,106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _08._Balanced_Parenthesis
  6. {
  7. class Program
  8. {
  9. static void Main()
  10. {
  11. string input = Console.ReadLine();
  12. Stack<char> parenthesStack = new Stack<char>();
  13. foreach (var symbol in input)
  14. {
  15. if (parenthesStack.Any())
  16. {
  17. char check = parenthesStack.Peek();
  18. if (check == '{' && symbol == '}')
  19. {
  20. parenthesStack.Pop();
  21. continue;
  22. }
  23. else if (check == '[' && symbol == ']')
  24. {
  25. parenthesStack.Pop();
  26. continue;
  27. }
  28. else if (check == '(' && symbol == ')')
  29. {
  30. parenthesStack.Pop();
  31. continue;
  32. }
  33. }
  34. parenthesStack.Push(symbol);
  35. }
  36. Console.WriteLine(!parenthesStack.Any() ? "YES" : "NO");
  37. }
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement