#1640

Check Array Formation Through Concatenation

pupil · 410 · lc easy +25 · verified · 57.3% accepted · 941 likes · top 53%

Description

You are given an array of distinct integers arr and a list of subarrays pieces (each with distinct values). Determine whether arr can be formed by concatenating pieces in any order without reordering elements within any piece. Return true if possible, false otherwise.

Example 1:

Input: arr = [15,88], pieces = [[88],[15]]
Output: true
Explanation: Concatenate [15] then [88]

Example 2:

Input: arr = [49,18,16], pieces = [[16,18,49]]
Output: false
Explanation: Even though the numbers match, we cannot reorder pieces[0].

Example 3:

Input: arr = [91,4,64,78], pieces = [[78],[4,64],[91]]
Output: true
Explanation: Concatenate [91] then [4,64] then [78]

Code

1
2
3