<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p>

<p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p>

<p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4.
Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}.
{&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1
<strong>Output:</strong> 2
<b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= strs.length &lt;= 600</code></li>
	<li><code>1 &lt;= strs[i].length &lt;= 100</code></li>
	<li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li>
	<li><code>1 &lt;= m, n &lt;= 100</code></li>
</ul>

class Solution:
    def findMaxForm(self, strs: List[str], m: int, n: int) -> int:
        # Can memoize the naive solution:
        # will need to stor the max length that we calculate the first time
        # wi

        def recur(strs_idx: int, prev_ones: int, prev_zeroes: int) -> int:
            # base case, at the end of the strs list
            # if so, then return 0
            if strs_idx == len(strs):
                return 0

            
            # can either choose the current item or not choose the current item
            # but need to check if the current item is a valid choice
            # Check if can choose current
            cur_ones = 0
            cur_zeroes = 0
            for char in strs[strs_idx]: 
                if char == "1":
                    cur_ones += 1
                else:
                    cur_zeroes += 1

            if (cur_ones + prev_ones, cur_zeroes + prev_zeroes) in dp[strs_idx]:
                return dp[strs_idx][(cur_ones + prev_ones, cur_zeroes + prev_zeroes)]
            # try and choose current
            choose_len = 0
            if (cur_ones + prev_ones <= n) and (cur_zeroes + prev_zeroes <= m):
                choose_len = recur(strs_idx + 1, cur_ones + prev_ones, cur_zeroes + prev_zeroes) + 1
            # can always not choose:
            dont_choose_len = recur(strs_idx + 1, prev_ones, prev_zeroes)

            result = max(dont_choose_len, choose_len)
            dp[strs_idx][(cur_ones + prev_ones, cur_zeroes + prev_zeroes)] = result
            return result


        # The dp will be a list of dicts
        # where each dict is a 1s/0s count
        dp = [{} for str in range(len(strs))]
        result = recur(0, 0, 0)
        return result


        # # want to select/not-select each item
        # # need to care about number of 1s/0s and the index in the string that we can select from
        # # then pick the max of selecting/not selecting

        # def recur(strs_idx: int, prev_ones: int, prev_zeroes: int) -> int:
        #     # base case, at the end of the strs list
        #     # if so, then return 0
        #     if strs_idx == len(strs):
        #         return 0
            
        #     # can either choose the current item or not choose the current item
        #     # but need to check if the current item is a valid choice

        #     # can always not choose:
        #     dont_choose_len = recur(strs_idx + 1, prev_ones, prev_zeroes)

        #     # Check if can choose current
        #     cur_ones = 0
        #     cur_zeroes = 0
        #     for char in strs[strs_idx]: 
        #         if char == "1":
        #             cur_ones += 1
        #         else:
        #             cur_zeroes += 1
        #     # try and choose current
        #     choose_len = 0
        #     if (cur_ones + prev_ones <= n) and (cur_zeroes + prev_zeroes <= m):
        #         choose_len = recur(strs_idx + 1, cur_ones + prev_ones, cur_zeroes + prev_zeroes) + 1

        #     result = max(dont_choose_len, choose_len)
        #     return result


        # result = recur(0, 0, 0)
        # return result
186806