jayati

Valid Parentheses

Nov 10th, 2022 (edited)
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     bool isValid(string s) {
  4.         stack<char> temp;
  5.         for(int i=0;i<s.length();i++)
  6.         {
  7.             if(temp.empty())
  8.             {
  9.                 temp.push(s[i]);
  10.             }
  11.             else if(temp.top()=='(' && s[i]==')' || temp.top()=='{' && s[i]=='}' || temp.top()=='[' && s[i]==']')
  12.             {
  13.                 temp.pop();
  14.                
  15.             }
  16.             else{
  17.                 temp.push(s[i]);
  18.             }
  19.         }
  20.         if(temp.empty())
  21.         {
  22.             return true;
  23.         }
  24.         else{
  25.             return false;
  26.         }
  27.        
  28.     }
  29. };
Advertisement
Add Comment
Please, Sign In to add comment