# [20\. Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) :::spoiler Solution ```cpp= class Solution { public: bool isValid(string s) { stack<int> stk; unordered_map<char, char> m = { {')', '('}, {']', '['}, {'}', '{'}, }; for (auto c : s) { if (m.count(c)) { if (!stk.empty() && stk.top() == m[c]) stk.pop(); else return false; } else stk.push(c); } return !stk.size(); } }; ``` - T: $O(N)$ - S: $O(N)$ :::
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up