Exemple #1
0
        public bool IsExpressionBalanced(string expression)
        {
            CustomStackArray <char> customStackLinkedList = new CustomStackArray <char>(expression.Length);

            foreach (char ch in expression)
            {
                if (isLeftBracket(ch))
                {
                    customStackLinkedList.Push(ch);
                }

                if (isRightBracket(ch))
                {
                    if (customStackLinkedList.IsEmpty())
                    {
                        return(false);
                    }

                    var top = customStackLinkedList.Pop();

                    if (!isBracketsMatched(top, ch))
                    {
                        return(false);
                    }
                }
            }

            return(customStackLinkedList.IsEmpty());
        }
Exemple #2
0
        public bool IsExpressionHaveDuplicateBraces(string expression)
        {
            CustomStackArray <char> customStackLinkedList = new CustomStackArray <char>(expression.Length);

            foreach (char ch in expression)
            {
                if (ch != ')')
                {
                    customStackLinkedList.Push(ch);
                }
                else
                {
                    if (customStackLinkedList.Peek() == '(')
                    {
                        return(true);
                    }

                    while (customStackLinkedList.Peek() != '(')
                    {
                        customStackLinkedList.Pop();
                    }

                    customStackLinkedList.Pop();
                }
            }

            return(false);
        }