knakul853

Untitled

Jul 20th, 2020
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. /**
  2. knakul853
  3. **/
  4.  
  5. class Solution {
  6. public:
  7.     bool isValid(string s) {
  8.        
  9.         stack<char>st;
  10.        
  11.         int n = (int)s.size();
  12.        
  13.         for(int i=0;i<n;i++)
  14.         {
  15.             if(s[i] =='{' || s[i] == '(' || s[i] =='[')
  16.             {
  17.                 st.push(s[i]);
  18.                 continue;
  19.             }
  20.            
  21.             if(st.size() <= 0) return 0;
  22.            
  23.             else{
  24.                
  25.                 char open = st.top();
  26.                 char close = s[i];
  27.                
  28.                 if(open  == '{' && close !='}'
  29.                    or open =='[' && close !=']'
  30.                    or open == '(' && close != ')'
  31.                   )
  32.                     return false;
  33.                
  34.                 st.pop();
  35.             }
  36.         }
  37.        
  38.         return st.empty();
  39.        
  40.     }
  41. };
Add Comment
Please, Sign In to add comment