Standard concise c++ solution


  • 0
    K
    class Solution {
    public:
        bool isValid(string s) {
            stack<char> left;
            for(int i=0; i<=s.size()-1; i++) {
                if (s[i] == '(' || s[i] == '[' || s[i] == '{')
                    left.push(s[i]);
                else {
                    if (left.empty()) return false;
                    char x = left.top();
                    left.pop();
                    if (x == '(' && s[i] != ')' || x == '{' && s[i] != '}' || x == '[' && s[i] != ']') return false;
                }
            }
            return left.empty();
        }
    };

Log in to reply
 

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