Beispiel #1
0
        public static bool CheckMachingBracket(string input)
        {
            GenericStack <char> stack = new GenericStack <char>();

            for (int i = 0; i < input.Length; i++)
            {
                char current = input[i];
                if (current == '[' || current == '{' || current == '(')
                {
                    stack.Push(current);
                }
                else if (current == ']' || current == '}' || current == ')')
                {
                    if (stack.IsEmpty())
                    {
                        return(false);
                    }
                    char x = stack.Pop();
                    if ((x == '(' && current != ')') || (x == '[' && current != ']') || (x == '{' && current != '}'))
                    {
                        return(false);
                    }
                }
            }
            if (stack.IsEmpty())
            {
                return(true);
            }

            return(false);
        }