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