Advertisement
Guest User

Untitled

a guest
Sep 8th, 2021
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace BalancedParentheses
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. string input = Console.ReadLine();
  11.  
  12. Stack<char> stackParentheses = new Stack<char>();
  13. bool isSuccess = true;
  14.  
  15. for (int i = 0; i < input.Length; i++)
  16. {
  17. if (input[i] == '{' || input[i] == '[' || input[i] == '(')
  18. {
  19. stackParentheses.Push(input[i]);
  20. }
  21. else
  22. {
  23. if (input[i] == '}' && stackParentheses.Pop() == '{' || input[i] == ']' && stackParentheses.Pop() == '[' || input[i] == ')' && stackParentheses.Pop() == '(')
  24. {
  25. isSuccess = true;
  26. }
  27. else
  28. {
  29. isSuccess = false;
  30. break;
  31. }
  32. }
  33. }
  34.  
  35. if (isSuccess == true)
  36. {
  37. Console.WriteLine("YES");
  38. }
  39. else
  40. {
  41. Console.WriteLine("NO");
  42. }
  43. }
  44. }
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement