Skip to content

1874. Form Array By Concatenating Subarrays Of Another Array

Difficulty: Medium

LeetCode Problem View on GitHub


1874. Form Array by Concatenating Subarrays of Another Array

Medium


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:

  • groups.length == n
  • 1 <= n <= 103
  • 1 <= groups[i].length, sum(groups[i].length) <= 103
  • 1 <= nums.length <= 103
  • -107 <= groups[i][j], nums[k] <= 107

Solution

class Solution {
    public boolean canChoose(int[][] groups, int[] nums) {
        int n = groups.length, m = groups[0].length;
        int currIdx = 0;
        RangeHash Hash = new RangeHash(nums);
        for (int i = 0; i < n; i++) {
            RangeHash reqHash = new RangeHash(groups[i]);
            boolean flag = false;
            for (int j = currIdx; j < nums.length; j++) {
                if (j + groups[i].length - 1 < nums.length) {
                    if (Hash.getRangeHash(j, j + groups[i].length - 1) == reqHash.getRangeHash(0, groups[i].length - 1)) {
                        currIdx = j + groups[i].length;
                        flag = true;
                        break;
                    }
                }
            }
            if (flag == false)
                return false;
        }
        return true;
    }
    public class RangeHash {
        private long[] prefixHash;
        private long[] powBase;
        private final int base = 31;
        private final int mod = 1_000_000_007;

        public RangeHash(int[] arr) {
            int n = arr.length;
            prefixHash = new long[n + 1];
            powBase = new long[n + 1];
            powBase[0] = 1;
            for (int i = 0; i < n; i++) {
                prefixHash[i + 1] = (prefixHash[i] * base + arr[i]) % mod;
                powBase[i + 1] = (powBase[i] * base) % mod;
            }
        }

        public long getRangeHash(int l, int r) {
            long hash = (prefixHash[r + 1] - prefixHash[l] * powBase[r - l + 1] % mod + mod) % mod;
            return hash;
        }
    }
}

Complexity Analysis

  • Time Complexity: O(?)
  • Space Complexity: O(?)

Approach

Detailed explanation of the approach will be added here