C++ 3 lines DFS


  • 0
    V
    bool checkValidString(string s, int p = 0, int v = 0) {
        for (; p < s.size() && v >= 0; v += s[p++] == '(' ? 1 : -1)
            if (s[p] == '*') return checkValidString(s, p + 1, v) ||  checkValidString(s, p + 1, v + 1) ||  checkValidString(s, p + 1, v - 1);
        return v == 0;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.