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 i^th subarray is equal to groups[i] (0-indexed), and if i > 0, the (i-1)^th subarray appears before the i^th 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
trueYou can choose the 0^th subarray as [1,-1,-1] and the 1^st subarray as [3,-2,0], and these subarrays are disjoint and in order.
Example 2
Input
groups = [[10,-2],[1,2,3,4]], nums = [1,2,3,4,10,-2]Output
falseChoosing [1,2,3,4] before [10,-2] is incorrect because the subarrays must appear in the same order as in groups, so [10,-2] must come before [1,2,3,4].
Constraints
- groups.length == n
- 1 <= n <= 10^3
- 1 <= groups[i].length, sum(groups[i].length) <= 10^3
- 1 <= nums.length <= 10^3
- -10^7 <= groups[i][j], nums[k] <= 10^7