<p>Given a string <code>s</code> containing only three types of characters: <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code> and <code>&#39;*&#39;</code>, return <code>true</code> <em>if</em> <code>s</code> <em>is <strong>valid</strong></em>.</p>

<p>The following rules define a <strong>valid</strong> string:</p>

<ul>
	<li>Any left parenthesis <code>&#39;(&#39;</code> must have a corresponding right parenthesis <code>&#39;)&#39;</code>.</li>
	<li>Any right parenthesis <code>&#39;)&#39;</code> must have a corresponding left parenthesis <code>&#39;(&#39;</code>.</li>
	<li>Left parenthesis <code>&#39;(&#39;</code> must go before the corresponding right parenthesis <code>&#39;)&#39;</code>.</li>
	<li><code>&#39;*&#39;</code> could be treated as a single right parenthesis <code>&#39;)&#39;</code> or a single left parenthesis <code>&#39;(&#39;</code> or an empty string <code>&quot;&quot;</code>.</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "()"
<strong>Output:</strong> true
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "(*)"
<strong>Output:</strong> true
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = "(*))"
<strong>Output:</strong> true
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 100</code></li>
	<li><code>s[i]</code> is <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code> or <code>&#39;*&#39;</code>.</li>
</ul>

class Solution {
public:
    bool checkValidString(string s) {
        return checkValidString_TopDown(s);
    }

    bool checkValidString_TopDown(const string& s) {
        return solve(s, 0, 0, {});
    }

    bool solve(const string& s, int index, int openCount, unordered_map<string, int>&& memo) {
        // memo[i][k] := when openCount of s[0..i-1] equals k, whether s[i..n-1] can close the parentheses
        // rules to close parentheses
        // 1) parentheses '(' comes before ')', or if we see ')' when openCount <= 0, it fails to close
        // 2) number of parentheses '(' is the same with ')' for closed pairs of parentheses
        // 3) when openCount <= 0, * can not close a parentheses, it can only open parentheses or be null
        int n = s.length();
        if (index == n) return openCount == 0;
        string key = to_string(index) + "#" + to_string(openCount);
        if (auto it = memo.find(key); it != memo.end()) {
            return it->second;
        }
        char c = s[index];
        if (openCount <= 0 && c == ')')
            return memo[key] = false;
        bool b = false;
        if (c == '*')
            b |= solve(s, index + 1, openCount, move(memo)),
            b |= solve(s, index + 1, openCount + 1, move(memo)),
            b |= openCount > 0 && solve(s, index + 1, openCount - 1, move(memo));
        else
            b |= (c == '('
                  ? solve(s, index + 1, openCount + 1, move(memo))
                  : solve(s, index + 1, openCount - 1, move(memo)));  // openCount > 0 is true for ')'
        return memo[key] = b;
    }

    // bool checkValidString2(string s) {
    //     int diffCount = 0, starsCount = 0;
    //     for (char c : s) {
    //         if (c == '(') diffCount++;
    //         else if (c == ')') diffCount--;
    //         else starsCount++;
    //     }
    //     if (starsCount < abs(diffCount)) return false;
    //     return solve(s, 0, 0, diffCount, starsCount);
    // }

    // bool solve2(const string& s, int index, int openCount, int diffCount, int starsCount) {
    //     if (index == s.length()) return openCount == 0;
    //     if (openCount < 0) return false;
    //     if (s[index] == '*') {
    //         if (openCount >= 0) {
    //             if (starsCount - abs(diffCount) > 0 &&    // star is null
    //                 solve(s, index + 1, openCount, diffCount, starsCount - 1))
    //                 return true;
    //             if ((diffCount < 0 || starsCount - abs(diffCount) > 0) &&
    //                 solve(s, index + 1, openCount + 1, diffCount + 1, starsCount - 1))
    //                 return true;
    //         }
    //         if (openCount > 0) {
    //             if ((diffCount > 0 || starsCount - abs(diffCount) > 0) &&
    //                 solve(s, index + 1, openCount - 1, diffCount - 1, starsCount - 1))
    //                 return true;
    //         }
    //     } else {
    //         if (openCount == 0 && s[index] == ')') return false;
    //         return s[index] == '('
    //                ? solve(s, index + 1, openCount + 1, diffCount, starsCount)
    //                : solve(s, index + 1, openCount - 1, diffCount, starsCount);
    //     }
    //     return false;
    // }
};
20780