<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 {
public:
    bool check(vector<int>& nums1, vector<int>& nums2, int len) {
        int n1 = nums1.size(), n2 = nums2.size();
        set<vector<int>> s;

        for (int i = 0; i < n1 - len + 1; i++) {
            vector<int> v(nums1.begin() + i, nums1.begin() + i + len);
            s.insert(v);
        }
        for (int i = 0; i < n2 - len + 1; i++) {
            vector<int> v(nums2.begin() + i, nums2.begin() + i + len);
            if (s.find(v) != s.end())
                return true;
        }
        return false;
    }

    bool check2(string& nums1, string& nums2, int len) {
        int n1 = nums1.size(), n2 = nums2.size();
        set<string> s;

        for (int i = 0; i < n1 - len + 1; i++) {
            s.insert(nums1.substr(i, len));
        }
        for (int i = 0; i < n2 - len + 1; i++) {
            if (s.find(nums2.substr(i, len)) != s.end())
                return true;
        }
        return false;
    }

    int findLength(vector<int>& nums1, vector<int>& nums2) {
        int n1 = nums1.size(), n2 = nums2.size();
        int lo = 0, hi = min(n1, n2);

        string s1,s2;
        for(int i = 0; i < n1; ++i){
            s1.push_back(nums1[i] + '0');
        }
        for(int i = 0; i < n2; ++i){
            s2.push_back(nums2[i] + '0');
        }

        while (lo < hi) {
            int mid = lo + (hi - lo + 1) / 2;
            if(check2(s1, s2, mid))
                lo = mid;
            else
                hi = mid - 1;
        }
        return lo;
    }
};
36260