Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- bool checkValidString(string s) {
- stack<int> left, star;
- for(int i = 0; i < s.length(); i++){
- if(s[i] == '('){
- left.push(i);
- }else if(s[i] == '*'){
- star.push(i);
- }else{
- if(left.empty() && star.empty()){
- return false;
- }
- if(!left.empty()){
- left.pop();
- }else{
- star.pop();
- }
- }
- }
- while(!left.empty() && !star.empty()){
- if(left.top() > star.top()){
- return false;
- }
- left.pop();
- star.pop();
- }
- return left.empty();
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment