# 20-Valid Parentheses ###### tags: `Easy` ## Question https://leetcode.com/problems/valid-parentheses/ ## Key ## Reference ## Solution ```cpp= class Solution { public: bool isValid(string s) { stack<char> stk; for (char c : s) { if (c == '(' || c == '[' || c == '{') { stk.push(c); } else { if (stk.empty() || c == ')' && stk.top() != '(' || c == ']' && stk.top() != '[' || c == '}' && stk.top() != '{') { return false; } stk.pop(); } } return stk.empty(); } }; ```
×
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