sundeepblue
4/12/2014 - 7:44 PM

Find if an expression containing [({ is syntactically correct.

Find if an expression containing [({ is syntactically correct.

bool check_parenthesis_correctness(const string& s) {
    if(s.empty()) return true;
    stack<char> stk;
    for(char c : s) {
        if(c == '{' || c == '[' || c == '(')
            stk.push(c);
        else {
            if(c == '}') {
                if(stk.empty() || stk.top() != '{') return false;
                stk.pop();
            } else if(c == ']') {
                if(stk.empty() || stk.top() != '[') return false;
                stk.pop();
            } else if(c == ')') {
                if(stk.empty() || stk.top() != '(') return false;
                stk.pop();
            }
        }
    }
    if(stk.empty() == false) return false;
    return true;
}