Advertisement
Guest User

Untitled

a guest
Feb 14th, 2022
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _8._Balanced_Parentheses
  6. {
  7. internal class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. string input = Console.ReadLine();
  12. Stack<string> stack = new Stack<string>();
  13. Queue<string> queue = new Queue<string>();
  14.  
  15. for (int i = 0; i < input.Length; i++)
  16. {
  17. if (i < input.Length / 2)
  18. {
  19. stack.Push(input[i].ToString()); continue;
  20. }
  21.  
  22. queue.Enqueue(input[i].ToString());
  23.  
  24. }
  25. bool isParenthesis = false;
  26. while (queue.Any())
  27. {
  28.  
  29. string currQ = queue.Dequeue();
  30. string currS = stack.Pop();
  31.  
  32.  
  33. if ((currS == "{" && currQ == "}")
  34. || (currS == "(" && currQ == ")")
  35. || (currS == "[" && currQ == "]"))
  36. { isParenthesis = true; continue; }
  37.  
  38. break;
  39. }
  40. Console.WriteLine(isParenthesis ? "YES" : "NO");
  41.  
  42. }
  43. }
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement