Ejemplo n.º 1
0
        //public string FirstNonRepetiveCharacter(string s)
        //{
        //    const occurrences = [];
        //    const charCodeBase = 'a'.charCodeAt(0);

        //    for (let i = 0; i < s.length; i++)
        //    {
        //        if (!occurrences[s.charCodeAt(i) - charCodeBase])
        //            occurrences[s.charCodeAt(i) - charCodeBase] = 1;
        //        else
        //            occurrences[s.charCodeAt(i) - charCodeBase]++;
        //    }

        //    for (let i = 0; i < s.length; i++)
        //    {
        //        let p = s.charCodeAt(i) - charCodeBase;
        //        if (occurrences[s.charCodeAt(i) - charCodeBase] === 1) return i;
        //    }

        //    return -1;
        //}

        public string BalancedBracket(string s)
        {
            _Stack <char> _stackLeft = new _Stack <char>();

            foreach (char c in s)
            {
                switch (c)
                {
                case '{':
                case '[':
                case '(':
                case '<':
                    _stackLeft.Push(c);
                    break;

                case '}':
                    if (_stackLeft.isEmpty() || _stackLeft.Peek() != '{')
                    {
                        return("NO");
                    }
                    _stackLeft.Pop();
                    break;

                case ']':
                    if (_stackLeft.isEmpty() || _stackLeft.Peek() != '{')
                    {
                        return("NO");
                    }
                    _stackLeft.Pop();
                    break;

                case ')':
                    if (_stackLeft.isEmpty() || _stackLeft.Peek() != '{')
                    {
                        return("NO");
                    }
                    _stackLeft.Pop();
                    break;

                case '>':
                    if (_stackLeft.isEmpty() || _stackLeft.Peek() != '{')
                    {
                        return("NO");
                    }
                    _stackLeft.Pop();
                    break;
                }
            }
            if (_stackLeft.isEmpty())
            {
                return("YES");
            }
            else
            {
                return("NO");
            }
        }
Ejemplo n.º 2
0
        public string ReverseString(string s)
        {
            var stack     = new _Stack <char>();
            var retString = new StringBuilder();

            foreach (var c in s)
            {
                stack.Push(c);
            }

            while (!stack.isEmpty())
            {
                retString.Append(stack.Pop());
            }

            return(retString.ToString());
        }