• + 0 comments

    C++:

    string isBalanced(string s) { std::map m {{'(',')'}, {'{','}'}, {'[',']'}}; std::stack st;

    if (s.length() %2 != 0)
        return("NO");
    for (int i=0;i<s.length();i++)
    {
        auto it = m.find(s[i]);
        if(it != m.end())
        {
            st.push(s[i]);
        }
        else
        {
            if(st.empty())
            {
                return("NO");
            }
            if(m[st.top()] == s[i])
                st.pop();
            else
                return("NO");
        }
    }
    if(st.empty())
        return("YES");
    else    
        return("NO");
    

    }