clueless coding // TODO: be smarter

LeetCode Valid Parentheses: CPP Solution



TL;DR Code Solution


bool isValid(string s) {

    unordered_map<char, char> map {
        {'}', '{'}, {']', '['}, {')', '('}
    };

    stack<char> stac;

    for (int i = 0; i < s.length(); i++) {
        if (map.find(s[i]) != map.end()) {
            if (stac.empty() || map[s[i]] != stac.top()) {
                return false;
            }
            stac.pop();
        }
        else {
            stac.push(s[i]);
        }
    }

    return stac.empty();

}