@ashish53v

I feel we can make it a bit faster by adding an additional check of max_cnt when looping through 2nd array,i.e, to check whether B_len-i > max_cnt. If max_cnt is greater than rest of the length of the array, then we just return the max_cnt right away.

class Solution {
public int findLength(int[] A, int[] B) {
int cnt=0,max_cnt=0;
int k = 0;
int A_len = A.length;
int B_len = B.length;
Map<Integer,List<Integer>> m = new HashMap<>();
for(int i=0;i<A_len;++i){
if(m.containsKey(A[i])) m.get(A[i]).add(i);
else{
List<Integer> l = new ArrayList<Integer>();
l.add(i);
m.put(A[i],l);
}
}
for(int i=0;i<B_len && (B_len-i) > max_cnt;++i){
if(m.containsKey(B[i])){
List<Integer> l = m.get(B[i]);
for(int idx:l){
if(A_len-idx < max_cnt) break;
k = 0;cnt = 0;
for(int j=idx;j<A_len && (i+k) < B_len;++j){
if(A[j] == B[i+k++]) cnt++;
else break;
}
max_cnt = max_cnt < cnt ? cnt : max_cnt;
}
}
}
return max_cnt;
}
}