Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- bool isValid(string s) {
- stack<char> st;
- for (const auto& c : s)
- {
- if (st.empty())
- {
- st.push(c);
- continue;
- }
- if (checkPair(st.top(), c)) st.pop();
- else st.push(c);
- }
- return st.empty();
- }
- bool checkPair(const char& x, const char& y)
- {
- if (x=='{' && y=='}') return true;
- if (x=='(' && y==')') return true;
- if (x=='[' && y==']') return true;
- return false;
- }
- };
Add Comment
Please, Sign In to add comment