<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:
    int findLength(vector<int>& nums1, vector<int>& nums2) {
        // return findLength_DP(nums1, nums2);
        return findLength_RollingHash(nums1, nums2);
    }

    int findLength_RollingHash(vector<int>& nums1, vector<int>& nums2) {
        int P = 113, kMod = 1e9 + 7;
        auto power = [](int a, int b, int M) -> int {  // (a^b) % M
            int ans = 1;
            for (int k = a % M; b; b >>= 1, k = (1L * k * k) % M) {
                if (b & 1) ans = (1L * ans * k) % M;
            }
            return ans;
        };
        auto hash = [&P, &kMod, &power](const vector<int>& A, int l) -> unordered_map<int, vector<int>> {  // return (hash, start)
            unordered_map<int, vector<int>> hashMap;  // hash(A) = sum_i{ P^i * A[i] | 0 <= i < n }
            int n = A.size(), invP = power(P, kMod - 2, kMod);
            for (int i = 0, sum = 0, k = 1; i < n; i++) {
                if (i >= l) sum = (1L * (sum - A[i - l]) * invP + 1L * k * A[i]) % kMod;  // roll window
                else sum = (sum + 1L * k * A[i]) % kMod;  // init sum
                if (i < l - 1) k = (1L * k * P) % kMod;  // update k: 1 -> P^(l-1)
                if (i >= l - 1) hashMap[sum].push_back(i - l + 1);  // code -> start pos
            }
            return hashMap;
        };
        auto check = [&nums1, &nums2](int start1, int start2, int len) -> bool {
            for (int i = 0; i < len; i++)
                if (nums1[start1 + i] != nums2[start2 + i])
                    return false;
            return true;
        };
        auto F = [&hash, &check, &nums1, &nums2](int len) -> bool {
            auto hashMap1 = hash(nums1, len);
            auto hashMap2 = hash(nums2, len);
            for (const auto& [code, startPoses] : hashMap1)
            if (auto it = hashMap2.find(code); it != hashMap2.end()) {
                for (auto const& start1 : startPoses)
                for (auto const& start2 : it->second)
                    if (check(start1, start2, len))
                        return true;
            }
            return false;
        };
        int m = nums1.size(), n = nums2.size();
        int maxLen = min(m, n), l = 0;
        for (int b = (maxLen + 1) / 2; b; b >>= 1) {
            while (l + b <= maxLen && F(l + b)) l += b;
        }
        return l;
    }

    int findLength_DP(vector<int>& nums1, vector<int>& nums2) {  // TC: O(M * N), SC: O(M * N)
        int m = nums1.size(), n = nums2.size();
        // dp[i][j] := maximum repeated subarray ending with i and j of the two arrays, 1-indexed
        // dp[0][0] = 0, dp[i][0] = 0, dp[0][j] = 0
        // dp[i][j] = dp[i-1][j-1]+1 if nums1[i]==nums2[j] else 0
        // answer = max_ij{ dp[i][j] | 1 <= i <= m and 1 <= j <= n }
        vector<vector<int>> dp(m + 1, vector<int>(n + 1));
        int ans = 0;
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                dp[i][j] = nums1[i - 1] == nums2[j - 1] 
                           ? dp[i - 1][j - 1] + 1
                           : 0;
                ans = max(ans, dp[i][j]);
            }
        }
        return ans;
    }
};
26848