Balanced Brackets

  • + 0 comments

    How the hell is this wrong?

    public static string isBalanced(string s)
        {
            bool isBalanced = true;
            int count = 0;
            Console.WriteLine(s);
            Stack myStack = new Stack();
            Dictionary<char,char> myDict = new Dictionary<char,char>();
            myDict.Add(')','(');
            myDict.Add('}','{');
            myDict.Add(']','[');
            for(int i = 0; i < s.Length; i++){
                if(myDict.ContainsKey(s[i]) && count > 0)
                {
                    if(Convert.ToChar(myStack.Pop())!=myDict.GetValueOrDefault(s[i]))
                        isBalanced = false;
                    count--;
                }
                else
                {
                     myStack.Push(s[i]);
                     count++;
                }
            }
            
            return (isBalanced) ? "YES" : "NO";
        }