Form Array by Concatenating Subarrays of Another Array - Problem

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 nums is 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

example_1.py โ€” Basic Match
$ Input: groups = [[1,2,3],[3,4]], nums = [7,7,1,2,3,4,7,7]
โ€บ Output: true
๐Ÿ’ก Note: We can find [1,2,3] starting at index 2 and [3,4] starting at index 5. Both subarrays are disjoint and in correct order.
example_2.py โ€” No Valid Arrangement
$ Input: groups = [[10,2],[1,2,3],[2,4]], nums = [1,2,3,4,10,2]
โ€บ Output: false
๐Ÿ’ก Note: We can find [1,2,3] at index 0, but then [10,2] appears at index 4, which violates the order requirement.
example_3.py โ€” Edge Case Single Elements
$ Input: groups = [[1],[2],[3]], nums = [1,2,3]
โ€บ Output: true
๐Ÿ’ก Note: Each group is a single element and they appear consecutively in order in nums.

Visualization

Tap to expand
๐Ÿš‚771233477Railway Yard: [7,7,1,2,3,4,7,7]123Seq 1:34Seq 2:โœ“ Found [1,2,3] at positions 2-4โœ“ Found [3,4] at positions 5-6Result: TRUE - All sequences found in order!
Understanding the Visualization
1
Setup
We have a blueprint of required carriage sequences and a railway yard
2
Scan
Walk through the yard looking for the first required sequence
3
Match
When found, mark those carriages as used and look for the next sequence
4
Result
Success if all sequences found in order, failure otherwise
Key Takeaway
๐ŸŽฏ Key Insight: Since groups must appear in order, we can use a greedy approach - always take the first valid match we find, as it never prevents finding a solution if one exists.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n * k)

Where n is length of nums and k is average group length. We scan nums once and do substring matching.

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using constant extra space for pointers and variables

n
2n
โœ“ Linear Space

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
Asked in
Google 45 Amazon 32 Microsoft 28 Meta 21
38.9K Views
Medium Frequency
~25 min Avg. Time
1.2K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen