Problem statement

https://binarysearch.com/problems/Piece-Grouping/

Solution

Equal to Leetcode 1640. Check Array Formation Through Concatenation, here we need linear time solution.

Complexity

It is O(n + m) for time and space, where n is lenth of arr and m is the total length of pieces.

Code

class Solution:
    def solve(self, pieces, arr):
        d = {x[0]: x for x in pieces if x}
        return list(chain(*[d.get(num, []) for num in arr])) == arr