Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 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. {
  10. string input = Console.ReadLine();
  11.  
  12. Queue<char> parenthesesQueue = new Queue<char>(input);
  13. Stack<char> parenthesesStack = new Stack<char>(input);
  14.  
  15. int counter = 0;
  16.  
  17. for (int i = 0; i < input.Length / 2; i++)
  18. {
  19. char symbolFromQueue = parenthesesQueue.Dequeue();
  20. char symbolFromStack = parenthesesStack.Pop();
  21. if (symbolFromQueue == '{' && symbolFromStack == '}')
  22. {
  23. counter++;
  24. }
  25. else if (symbolFromQueue == '(' && symbolFromStack == ')')
  26. {
  27. counter++;
  28. }
  29. else if (symbolFromQueue == '[' && symbolFromStack == ']')
  30. {
  31. counter++;
  32. }
  33. }
  34.  
  35. if (counter == input.Length / 2)
  36. {
  37. Console.WriteLine("YES");
  38. }
  39. else
  40. {
  41. Console.WriteLine("NO");
  42. }
  43. }
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement