• + 0 comments

    Trying to be compact:

    public static string isBalanced(string s)
    {
        Stack<char> st = new();
        foreach (char chr in s.ToArray())
        {   
            if (new char[]{'(','[','{'}.Contains(chr)) st.Push(chr);
            else if ( st.Count > 0 && (int)chr > (int)st.Peek() && (int)chr-2 <= (int)st.Pop()) continue;
            else return "NO";  
        }   
        
        return (st.Count > 0)?"NO":"YES";
    }