C# Solution


  • 0
            public bool IsValid(string s)
            {
                Stack<char> sign = new Stack<char>();
    
                foreach (var item in s.ToCharArray())
                    if (item == '(')
                        sign.Push(')');
                    else if (item == '[')
                        sign.Push(']');
                    else if (item == '{')
                        sign.Push('}');
                    else if (sign.Count == 0 || sign.Pop() != item)
                        return false;
    
                return sign.Count == 0;
            }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.