2-D Dynamic Programming Two strings
559. Maximum Length of Repeated Subarray
You are given two arrays of integers, nums1 and nums2. Return the length of the longest subarray that
appears in both of them.
A subarray is a run of elements that sit next to each other. [2,7,1] is a subarray of [4,2,7,1,9],
but [2,1] is not, because the 7 sits between the 2 and the 1. The two copies of the subarray may start at
different indexes.
If no value appears in both arrays, the answer is 0.
Example 1
Input: nums1 = [4, 2, 7, 7, 1, 9, 3]
nums2 = [7, 1, 9, 3, 2, 4, 2, 7]
4Explanation: [7,1,9,3] is a subarray of both: it starts at index 3 of nums1 and at index 0 of nums2. [4,2,7] is
common too, but shorter.
Example 2
Input: nums1 = [0, 0, 0, 0], nums2 = [0, 0]
2Explanation: All of nums2 is a subarray of nums1, and no common subarray can be longer than nums2.
Example 3
Input: nums1 = [1, 2, 3], nums2 = [4, 5, 6]
0Explanation: The arrays share no value, so they have no common subarray.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ nums1.length, nums2.length ≤ 30000 ≤ nums1[i], nums2[i] ≤ 100
Clarifying questions
What you would ask an interviewer before writing any code.