Advertisement
atulsingh7890

Valid Parenthesis

Jan 20th, 2021
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. //  https://leetcode.com/explore/challenge/card/january-leetcoding-challenge-2021/581/week-3-january-15th-january-21st/3610/
  2. class Solution {
  3. public:
  4.     bool isValid(string s) {
  5.         std::stack<char> st;
  6.         for(auto ch : s) {
  7.             if(ch == '(' || ch == '{' || ch == '[') {
  8.                 st.push(ch);
  9.             } else if(ch == ')') {
  10.                 if(st.size() <= 0 || st.top() != '(')
  11.                     return false;
  12.                 st.pop();
  13.             } else if(ch == '}') {
  14.                 if(st.size() <= 0 || st.top() != '{')
  15.                     return false;
  16.                 st.pop();
  17.             } else if(ch == ']') {
  18.                 if(st.size() <= 0 || st.top() != '[')
  19.                     return false;
  20.                 st.pop();
  21.             }
  22.         }
  23.         return st.empty();
  24.     }
  25. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement