<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 solve(string &s, int i, int cnt){
        if(cnt < 0) return 0;
        if(i == s.size()){
            if(cnt == 0) return 1;
            return 0;
        }

        if(s[i] == '('){
            return solve(s, i+1, cnt+1);
        }
        else if(s[i] == ')'){
            return solve(s, i+1, cnt-1);
        }
        return (solve(s, i+1, cnt+1) || solve(s, i+1, cnt-1) || solve(s, i+1, cnt));
    }

    //memoization
    bool solveTD(string &s, int i, int cnt, vector<vector<int>> &dp){
        if(cnt < 0) return 0;
        if(i == s.size()){
            if(cnt == 0) return 1;
            return 0;
        }
        if(dp[i][cnt] != -1) return dp[i][cnt];

        if(s[i] == '('){
            return dp[i][cnt] = solveTD(s, i+1, cnt+1, dp);
        }
        else if(s[i] == ')'){
            return dp[i][cnt] = solveTD(s, i+1, cnt-1, dp);
        }
        return dp[i][cnt] = (solveTD(s, i+1, cnt+1, dp) || solveTD(s, i+1, cnt-1, dp) || solveTD(s, i+1, cnt, dp));
    }
    //BU
    bool solve_BU(string &s) {
        int slen = s.length();
        vector<vector<int>> dp(slen + 1, vector<int>(slen + 1, 0));
        
        // Base case: empty string is valid with zero unmatched opening parentheses
        dp[slen][0] = 1;

        // Fill the DP table from the end to the start
        for (int i = slen - 1; i >= 0; i--) {
            for (int cnt = 0; cnt <= slen; cnt++) {
                if (s[i] == '(') {
                    if (cnt + 1 <= slen) { // Ensure index is within bounds
                        dp[i][cnt] = dp[i + 1][cnt + 1];
                    }
                } 
                else if (s[i] == ')') {
                    if (cnt > 0) { // Ensure we have at least one unmatched '(' to match with ')'
                        dp[i][cnt] = dp[i + 1][cnt - 1];
                    }
                } 
                else { // For '*'
                    // Case 1: Treat '*' as '('
                    if (cnt + 1 <= slen) {
                        dp[i][cnt] = dp[i + 1][cnt + 1];
                    }
                    // Case 2: Treat '*' as ')'
                    if (cnt > 0) {
                        dp[i][cnt] = dp[i][cnt] || dp[i + 1][cnt - 1];
                    }
                    // Case 3: Treat '*' as an empty string
                    dp[i][cnt] = dp[i][cnt] || dp[i + 1][cnt];
                }
            }
        }
        return dp[0][0];
    }
    bool checkValidString(string s) {
        int i = 0;
        int cnt = 0;
        int slen = s.length();
        vector<vector<int>> dp(slen, vector<int>(slen, -1));
        // return solveTD(s, i, cnt, dp);
        return solve_BU(s);
    }
};
11260