Ali 1089. Valid Parenthesis String
class Solution {
public:
/**
* @param s: the given string
* @return: whether this string is valid
*/
bool checkValidString(string &s) {
// Write your code here
const int size = s.size();
if(size == 0){
return true;
}
int left = 0;
for(int i = 0; i < size; i++){
if(s[i] == '(' || s[i] == '*'){
left++;
}
else{
left--;
}
if(left < 0){
return false;
}
}
int right = 0;
for(int i = size - 1; i >= 0; i--){
if(s[i] == ')' || s[i] == '*'){
right++;
}
else{
right--;
}
if(right < 0){
return false;
}
}
return true;
}
};
Comments
Post a Comment