Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- public class Program
- {
- private static Dictionary<char, char> Symbols = new Dictionary<char, char>()
- {
- { '[', ']'},
- { ']', '['},
- { '(', ')'},
- { ')', '('},
- { '}', '{'},
- { '{', '}'},
- };
- private static Stack<char> SymbolStack = new Stack<char>();
- private static bool IsBracket(char InChar)
- {
- return Symbols.ContainsKey(InChar);
- }
- private static bool IsCloseBracket(char InChar)
- {
- return InChar == ']' || InChar == '}' || InChar == ')';
- }
- private static bool IsOpenBracket(char InChar)
- {
- return InChar == '[' || InChar == '{' || InChar == '(';
- }
- public static string Check(string Input)
- {
- if (Input == null || Input.Length == 0)
- {
- return "1";
- }
- SymbolStack.Clear();
- StringBuilder CustomString = new StringBuilder(Input);
- for (int I = 0; I < Input.Length; ++I)
- {
- char Symbol = CustomString[I];
- if (!IsBracket(Symbol)) continue;
- if (IsOpenBracket(Symbol))
- {
- SymbolStack.Push(Symbol);
- }
- if (IsCloseBracket(Symbol))
- {
- if (SymbolStack.Count > 0 && Symbols[SymbolStack.Peek()] == Symbol)
- {
- SymbolStack.Pop();
- }
- else
- {
- return (I + 1).ToString();
- }
- }
- }
- if (SymbolStack.Count > 0)
- {
- if (IsOpenBracket(SymbolStack.Peek()))
- {
- return (Input.IndexOf(SymbolStack.Peek()) + 1).ToString();
- }
- else
- {
- return Input.Length.ToString();
- }
- }
- return "Success";
- }
- public static void Main()
- {
- string Input = Console.ReadLine();
- Console.WriteLine(Check(Input));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement