classSolution:deffindLength(self,A: List[int],B: List[int]) ->int: n, m =len(A),len(B)if n > m: A, B = B, A n, m = m, n cache =0 max_len =0for i inrange(-(n -1), m):for j inrange(n):if i <0or i >= m: i +=1continueif A[j]== B[i]:if i ==0or j ==0: cache =1else: cache +=1 max_len =max(max_len, cache)else: cache =0 i +=1return max_len
classSolution:deffindLength(self,A: List[int],B: List[int]) ->int: n, m =len(A),len(B) res =0defcount_length(length,a_start,b_start): r, k =0,0 ta, tb = A[a_start:], B[b_start:]for i inrange(length):if ta[i]== tb[i]: k +=1 r =max(r, k)else: k =0return rfor i inrange(n): res =max(res, count_length(min(m, n - i), i, 0))for i inrange(m): res =max(res, count_length(min(n, m - i), 0, i))return res