Easy solutiion withRuntime: 0 ms, faster than 100.00% of C++ online submissions
class Solution {
public:
    bool isValid(string s) {
       stack<int> st;
        st.push(1);
        for (int i = 0; i < s.size(); i++)
        {
            if (int(s[i]) == st.top() + 1 || int(s[i]) == st.top() + 2 )
            {
                st.pop();
            }
            else
            {
                st.push(int(s[i]));
            }
        }
        if (st.top() == 1 || st.empty())
        {
            return true;
        }
        return false;
    }
};
Comments (0)