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