Advertisement
GabrielDas

Untitled

May 18th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace P08BalancedParantheses
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. string input = Console.ReadLine();
  12. // bool isBalanced = true;
  13.  
  14. var stack = new Stack<char>();
  15.  
  16. if(input.Length == 0)
  17. {
  18. Console.WriteLine("NO");
  19. return;
  20. }
  21. else if (input.Length % 2 != 0)
  22. {
  23. Console.WriteLine("NO");
  24. return;
  25. }
  26.  
  27. for (int i = 0; i < input.Length / 2; i++)
  28. {
  29. stack.Push(input[i]);
  30.  
  31. }
  32. // Console.WriteLine(string.Join(" ", stack));
  33.  
  34. for (int j = 0; j < input.Length / 2; j++)
  35. {
  36. char symbolToCheck = stack.Peek();
  37.  
  38. if ((symbolToCheck == '[' && input[j + input.Length / 2] == ']')
  39. || (symbolToCheck == '{' && input[j + input.Length / 2] == '}')
  40. || (symbolToCheck == '(' && input[j + input.Length / 2] == ')'))
  41. {
  42. stack.Pop();
  43. }
  44. else
  45. {
  46. Console.WriteLine("NO");
  47. break;
  48.  
  49. }
  50.  
  51. }
  52.  
  53. if (stack.Count == 0)
  54. {
  55. Console.WriteLine("YES");
  56. }
  57.  
  58.  
  59. }
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement