718. Maximum Length of Repeated Subarray

Given two integer arrays A and B, return the maximum length of an 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. 1 <= len(A), len(B) <= 1000
  2. 0 <= A[i], B[i] < 100

 

Rust Solution

struct Solution;

impl Solution {
    fn find_length(a: Vec<i32>, b: Vec<i32>) -> i32 {
        let n = a.len();
        let m = b.len();
        let mut dp: Vec<Vec<i32>> = vec![vec![0; m + 1]; n + 1];
        let mut res = 0;
        for i in 0..n {
            for j in 0..m {
                if a[i] == b[j] {
                    dp[i + 1][j + 1] = dp[i][j] + 1;
                    res = res.max(dp[i + 1][j + 1]);
                }
            }
        }
        res
    }
}

#[test]
fn test() {
    let a = vec![1, 2, 3, 2, 1];
    let b = vec![3, 2, 1, 4, 7];
    let res = 3;
    assert_eq!(Solution::find_length(a, b), res);
}

Having problems with this solution? Click here to submit an issue on github.