Very Straight Forward Java solution (90ms)

  • 0

    Guess it is O(n*m^2). Loop through both array, and when we have a match element, find the longest substring started from that element. And since this is the start element of the substring, the element before this one could not be matched element.

    public int findLength(int[] A, int[] B) {
        int res=0;
        for(int i=0;i<A.length;i++){
            for(int j=0;j<B.length;j++){
                if(B[j]==A[i] && (i==0 || j==0 || B[j-1]!=A[i-1])){
                    int k=1;
                    while(k<A.length-i && k< B.length-j && B[j+k]==A[i+k]){
        return res;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.