siddhant07

Valid Parenthesis String

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