A short and easy solution in c++


  • 0
    L

    bool isValid(string s) {
    if (s == "") return false;
    stack<char> cache;
    for (char c : s) {
    if (c == ')' || c == '}' || c == ']') {
    if (cache.size() == 0) return false;
    if (cache.top() == c - 1 || cache.top() == c - 2) cache.pop();
    else return false;
    continue;
    }
    cache.push(c);
    }
    return cache.size()==0;
    }


Log in to reply
 

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