# Longest Common Substring — Day 57(Python)

Today’s problem is on Dynamic Programming. Let us look into the question without wasting much time.

**718****. Maximum Length of Repeated Subarray**

Given two integer arrays `A`

and `B`

, return the maximum length of the subarray that appears in both arrays.

**Example 1:**

**Input:**

A: [1,2,3,2,1]

B: [3,2,1,4,7]

**Output:** 3

**Explanation:**

The repeated subarray with maximum length is [3, 2, 1].

**Note:**

- 1 <= len(A), len(B) <= 1000
- 0 <= A[i], B[i] < 100

Let us analyze the question. We are given 2 input lists, and we need to find the longest repeated subarray. Isn’t it similar to Longest Common Subsequence? The only difference between both the questions is, the current question wants the longest subarray and LCS requires us to find the longest subsequence. What is the difference between subarray and subsequence?

According to GeeksForGeeks,

A subarray is a

contiguouspart of an array. An array that is inside another array. For example, consider the array [1, 2, 3, 4], There are 10 non-empty sub-arrays. The subarrays are (1), (2), (3), (4), (1,2), (2,3), (3,4), (1,2,3), (2,3,4) and (1,2,3,4).A subsequence is a sequence that can be derived from another sequence by zero or more elements, without changing the order of the remaining elements.

For the same example, there are 15 sub-sequences. They are (1), (2), (3), (4), (1,2), (1,3),(1,4), (2,3), (2,4), (3,4), (1,2,3), (1,2,4), (1,3,4), (2,3,4), (1,2,3,4).

The only difference between subarray and subsequence is continuity. In the given question, we need to find the longest common subarray from the given 2 input list. Before we jump into finding the solution, let us recall the code for LCS using the Bottom-Up approach.

`class FindLongest:`

def longestCommonSubsequence(self, text1: str, text2: str) -> int:

memo = [[float('-inf') for j in range(len(text2)+1)]for i in range(len(text1)+1)]

for i in range(len(memo)):

for j in range(len(memo[0])):

if i == 0 or j == 0:

memo[i][j] = 0

elif text1[i-1] == text2[j-1]:

memo[i][j] = 1 + memo[i-1][j-1]

else:

memo[i][j] = max(memo[i-1][j], memo[i][j-1])

return memo[-1][-1]

Similar to LCS we will be creating a 2D matrix of the size of both the input array. We will be initializing the matrix with INT MIN value.

`memo = [[float('-inf') for j in range(len(B)+1)] for i in range(len(A)+1)]`

Next, we will be filling our matrix with the calculated values.

We know when any of the arrays are empty, there cannot be anything that we can term as common. Since the 1st row represents an empty array and the 1st column represents an empty array, we will be initializing both with 0.

`for i in range(len(memo)):`

for j in range(len(memo[0])):

if(i==0 or j==0):

memo[i][j]=0

We will move to the main algorithm.

From LCS we know, if the element at index i-1 in array1 is equal to the elements at index j-1 in array2, then the current cell value will be 1+memo[i-1][j-1]. The same happens in this case too.

`if A[i-1] == B[j-1]:`

memo[i][j] = memo[i-1][j-1] +1

What if the elements are not equal? Since we are trying to find a subarray, in such a case, we are breaking our continuity, and hence, we will fill those cell values with 0.

`else:`

memo[i][j] = 0

In the end, we will be returning the maximum value present in our matrix.

Let us look into the complete code snippet.

`class LongestSubarray:`

def findLength(self, A: List[int], B: List[int]) -> int:

memo = [[float('-inf') for j in range(len(B)+1)] for i in range(len(A)+1)]

ans = 0

for i in range(len(memo)):

for j in range(len(memo[0])):

if i == 0 or j == 0:

memo[i][j] = 0

elif A[i-1] == B[j-1]:

memo[i][j] = memo[i-1][j-1] +1

else:

memo[i][j] = 0

ans = max(ans, memo[i][j])

return ans

**Complexity analysis.**

**Time Complexity**

We are traversing through a 2D array of size M*N, hence the time complexity is O(M*N), where M is the size of array1 and N is the size of array2.

**Space Complexity.**

We are creating a 2D array of size M*N, hence the space complexity is O(M*N), where M is the size of array1 and N is the size of the array2.

Link to Aditya Verma’s video