Imagine you have a blueprint of several ordered building blocks (the groups array) and a long strip of numbered tiles (the nums array). Your task is to find if you can select non-overlapping consecutive segments from the strip that perfectly match each building block in the exact same order.
The Challenge: Given a 2D array groups containing n target sequences and an array nums, determine if you can find n disjoint subarrays in nums such that:
- The i-th subarray exactly matches
groups[i] - The subarrays appear in the same order as the groups (no backtracking)
- No element from
numsis used more than once
Example: If groups = [[1,2,3],[3,4]] and nums = [7,7,1,2,3,4,7,7], you can find [1,2,3] starting at index 2 and [3,4] starting at index 5, so return true.
Input & Output
Visualization
Time & Space Complexity
Where n is length of nums and k is average group length. We scan nums once and do substring matching.
Only using constant extra space for pointers and variables
Constraints
- 1 โค groups.length โค 103
- 1 โค groups[i].length โค 106
- 1 โค nums.length โค 106
- 1 โค groups[i][j], nums[k] โค 107
- Sum of all groups[i].length โค 106