<p>Given two integer arrays <code>nums1</code> and <code>nums2</code>, return <em>the maximum length of a subarray that appears in <strong>both</strong> arrays</em>.</p>

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

<pre>
<strong>Input:</strong> nums1 = [1,2,3,2,1], nums2 = [3,2,1,4,7]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The repeated subarray with maximum length is [3,2,1].
</pre>

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

<pre>
<strong>Input:</strong> nums1 = [0,0,0,0,0], nums2 = [0,0,0,0,0]
<strong>Output:</strong> 5
<strong>Explanation:</strong> The repeated subarray with maximum length is [0,0,0,0,0].
</pre>

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

<ul>
	<li><code>1 &lt;= nums1.length, nums2.length &lt;= 1000</code></li>
	<li><code>0 &lt;= nums1[i], nums2[i] &lt;= 100</code></li>
</ul>

class Solution {
    bool check(int length, string& a, string& b) {
        unordered_set<string> seen;
        for (int i = 0; i <= (int)a.length() - length; i++) {
            seen.insert(a.substr(i, length));
        }
        for (int i = 0; i <= (int)b.length() - length; i++) {
            if (seen.count(b.substr(i, length))) {
                return true;
            }
        }
        return false;
    }
public:
    int findLength(vector<int>& nums1, vector<int>& nums2) {
        int m = nums1.size(), n = nums2.size();
        string a = "", b = "";
        for (int num : nums1) {
            a += static_cast<char>(num);
        }
        for (int num : nums2) {
            b += static_cast<char>(num);
        }
        // 0 is always possible ... return upper left bound
        int l = 0, r = min(a.length(), b.length());
        while (l < r) {
            int mid = l + (r - l + 1) / 2;
            if (check(mid, a, b)) {
                l = mid;
            } else {
                r = mid - 1;
            }
        }
        return l;
        /*
        vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
        // dp[i][j] = max length starting from i and j...
        // if (nums1[i] == nums2[j]) dp[i][j] = dp[i + 1][j + 1] + 1
        int maxLength = 0;
        for (int i = m - 1; i >= 0; i--) {
            for (int j = n - 1; j >= 0; j--) {
                if (nums1[i] == nums2[j]) {
                    dp[i][j] = dp[i + 1][j + 1] + 1;
                    maxLength = max(maxLength, dp[i][j]);
                } else {
                    dp[i][j] = 0;
                }
            }
        }
        return maxLength;
        */
        /*
        int maxLength = 0;
        for (int i = 0; i < nums1.size(); i++) {
            for (int j = 0; j < nums2.size(); j++) {
                int k = 0;
                while (i + k < nums1.size() && j + k < nums2.size()) {
                    if (nums1[i + k] == nums2[j + k]) {
                        k++;
                    } else {
                        break;
                    }
                }
                maxLength = max(maxLength, k);

            }
        }
        return maxLength;
        */
    }
};
35319