使用标准的搜索穷举方式, 时间复杂度略高,会超时
class Solution {
public:
bool checkValidString(string s) {
return helper(s,0,0,0);
}
bool helper(string s, int index, int left, int right){
for(int i=index;i<s.size();i++){
if(left<right) return false;
if(s[i]=='(') left++;
else if(s[i]==')') right++;
else if(s[i]=='*'){
if(helper(s,i+1,left,right)||helper(s,i+1,left+1,right)||helper(s,i+1,left,right+1)) return true;
}
}
return left==right;
}
};