Form Array by Concatenating Subarrays of Another Array

You are given a 2D integer array groups of length n. You are also given an integer array nums.

You are asked if you can choose n disjoint subarrays from the array nums such that the ith subarray is equal to groups[i] (0-indexed), and if i > 0, the (i-1)th subarray appears before the ith subarray in nums (i.e. the subarrays must be in the same order as groups).

Return true if you can do this task, and false otherwise.

Note that the subarrays are disjoint if and only if there is no index k such that nums[k] belongs to more than one subarray. A subarray is a contiguous sequence of elements within an array.

 

Example 1:

Input: groups = [[1,-1,-1],[3,-2,0]], nums = [1,-1,0,1,-1,-1,3,-2,0]
Output: true
Explanation: You can choose the 0th subarray as [1,-1,0,1,-1,-1,3,-2,0] and the 1st one as [1,-1,0,1,-1,-1,3,-2,0].
These subarrays are disjoint as they share no common nums[k] element.

Example 2:

Input: groups = [[10,-2],[1,2,3,4]], nums = [1,2,3,4,10,-2]
Output: false
Explanation: Note that choosing the subarrays [1,2,3,4,10,-2] and [1,2,3,4,10,-2] is incorrect because they are not in the same order as in groups.
[10,-2] must come before [1,2,3,4].

Example 3:

Input: groups = [[1,2,3],[3,4]], nums = [7,7,1,2,3,4,7,7]
Output: false
Explanation: Note that choosing the subarrays [7,7,1,2,3,4,7,7] and [7,7,1,2,3,4,7,7] is invalid because they are not disjoint.
They share a common elements nums[4] (0-indexed).

 

Constraints:


Solution:

dfs

class Solution {
    Boolean[][] dp;
    
    public boolean canChoose(int[][] groups, int[] nums) {
        dp = new Boolean[groups.length][nums.length];
        return dfs(groups, nums, 0, 0);        
    }
    
    private boolean dfs(int[][] groups, int[] nums, int i, int j) {
        int n = groups.length, m = nums.length;
        if (i == n) return true;
        if (j == m) return false;
        if (dp[i][j] != null) return dp[i][j];
        int[] group = groups[i];
        for (int p = j; p + group.length <= nums.length; p ++) {
            boolean valid = true;
            for (int q = 0; q < group.length; q ++) {
                if (nums[p + q] != group[q]) {
                    valid = false;
                    break;
                }
            }
            if (valid && dfs(groups, nums, i + 1, p + group.length)) {
                return dp[i][j] = true;
            }
        }
        return dp[i][j] = false;
    }
}

greedy

class Solution {
    public boolean canChoose(int[][] groups, int[] nums) {
        int i=0;
        for(int start =0;i < groups.length && groups[i].length+start <= nums.length;start++)
            if(search(groups[i], nums, start)) 
                start += groups[i++].length - 1;
        return i==groups.length;
    }
    private boolean search(int[] group, int[] nums, int start) {
        for(int i=0;i<group.length;i++) 
            if(group[i] != nums[i+start])
                return false;
        return true;
    }
}