Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // https://leetcode.com/explore/challenge/card/january-leetcoding-challenge-2021/581/week-3-january-15th-january-21st/3610/
- class Solution {
- public:
- bool isValid(string s) {
- std::stack<char> st;
- for(auto ch : s) {
- if(ch == '(' || ch == '{' || ch == '[') {
- st.push(ch);
- } else if(ch == ')') {
- if(st.size() <= 0 || st.top() != '(')
- return false;
- st.pop();
- } else if(ch == '}') {
- if(st.size() <= 0 || st.top() != '{')
- return false;
- st.pop();
- } else if(ch == ']') {
- if(st.size() <= 0 || st.top() != '[')
- return false;
- st.pop();
- }
- }
- return st.empty();
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement