Ali 1089. Valid Parenthesis String

Code(Language:C++) (Judger:cloudjudge-cluster-9)
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

Popular posts from this blog

Amazon OA 763. Partition Labels

1427. Split Array into Fibonacci Sequence

05/25 周一