Easy to understand 0ms C++ solution


  • 0
    P
    class Solution {
    public:
        bool isValid(string s) {
            if (s.empty() || s.size() == 1) return false;
            
            stack<char> stacky;
            
            for(string::iterator it = s.begin(); it != s.end(); it++) {
                if (stacky.empty()) stacky.push(*it);
                
                else if (stacky.top() == *it - 1 || stacky.top() == *it - 2) stacky.pop();
                
                else stacky.push(*it);
            }
            
            return stacky.empty();
        }
    };

Log in to reply
 

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