# 718. Maximum Length of Repeated Subarray

Given two integer arrays `nums1` and `nums2`, return *the maximum length of a subarray that appears in **both** arrays*.

&#x20;

**Example 1:**

```
Input: nums1 = [1,2,3,2,1], nums2 = [3,2,1,4,7]
Output: 3
Explanation: The repeated subarray with maximum length is [3,2,1].
```

**Example 2:**

```
Input: nums1 = [0,0,0,0,0], nums2 = [0,0,0,0,0]
Output: 5
```

&#x20;

**Constraints:**

* `1 <= nums1.length, nums2.length <= 1000`
* `0 <= nums1[i], nums2[i] <= 100`

**Solution:**

和LCS <https://leetcode.com/problems/longest-common-subsequence/>做法类似，如果当前两个string相等就把当前格子变成\[i - 1]\[j - 1] + 1。不相等就保留0。

```
public class Solution {
    public int findLength(List<Integer> A, List<Integer> B) {
        int aSize = A.size();
        int bSize = B.size();
        int[][] dp = new int[aSize + 1][bSize + 1];
        int result = 0;
        for (int i = 1; i < dp.length; i++) {
            for (int j = 1; j < dp[i].length; j++) {
                dp[i][j] = A.get(i - 1) == B.get(j - 1) ? dp[i - 1][j - 1] + 1 : 0;
                result = Math.max(result, dp[i][j]);
            }
        }
        return result;
    }
}
```
