Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. public class Program
  6. {
  7. private static Dictionary<char, char> Symbols = new Dictionary<char, char>()
  8. {
  9. { '[', ']'},
  10. { ']', '['},
  11. { '(', ')'},
  12. { ')', '('},
  13. { '}', '{'},
  14. { '{', '}'},
  15. };
  16.  
  17. private static Stack<char> SymbolStack = new Stack<char>();
  18.  
  19. private static bool IsBracket(char InChar)
  20. {
  21. return Symbols.ContainsKey(InChar);
  22. }
  23.  
  24. private static bool IsCloseBracket(char InChar)
  25. {
  26. return InChar == ']' || InChar == '}' || InChar == ')';
  27. }
  28.  
  29. private static bool IsOpenBracket(char InChar)
  30. {
  31. return InChar == '[' || InChar == '{' || InChar == '(';
  32. }
  33.  
  34. public static string Check(string Input)
  35. {
  36. if (Input == null || Input.Length == 0)
  37. {
  38. return "1";
  39. }
  40.  
  41. SymbolStack.Clear();
  42.  
  43. StringBuilder CustomString = new StringBuilder(Input);
  44.  
  45. for (int I = 0; I < Input.Length; ++I)
  46. {
  47. char Symbol = CustomString[I];
  48.  
  49. if (!IsBracket(Symbol)) continue;
  50.  
  51. if (IsOpenBracket(Symbol))
  52. {
  53. SymbolStack.Push(Symbol);
  54. }
  55.  
  56. if (IsCloseBracket(Symbol))
  57. {
  58. if (SymbolStack.Count > 0 && Symbols[SymbolStack.Peek()] == Symbol)
  59. {
  60. SymbolStack.Pop();
  61. }
  62. else
  63. {
  64. return (I + 1).ToString();
  65. }
  66. }
  67. }
  68.  
  69. if (SymbolStack.Count > 0)
  70. {
  71. if (IsOpenBracket(SymbolStack.Peek()))
  72. {
  73. return (Input.IndexOf(SymbolStack.Peek()) + 1).ToString();
  74. }
  75. else
  76. {
  77. return Input.Length.ToString();
  78. }
  79. }
  80.  
  81. return "Success";
  82. }
  83.  
  84.  
  85.  
  86. public static void Main()
  87. {
  88.  
  89. string Input = Console.ReadLine();
  90.  
  91. Console.WriteLine(Check(Input));
  92.  
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement