0ms C++ using map


  • 0
    Y
    class Solution {
    public:
        bool isValid(string s) {
            stack<char> res;
            map<char, char> mp={{'(', ')'}, {'[', ']'}, {'{', '}'}};
            for (auto par:s) {
                if (mp.count(par)) {
                    res.push(par);
                }
                else {
                    if (res.empty() || mp[res.top()] != par)
                        return false;
                    res.pop();
                }
            }
            return res.empty();
        }
    };
    

Log in to reply
 

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