Leetcode 1760 Solution

This article provides solution to leetcode question 1760 (check-array-formation-through-concatenation)

https://leetcode.com/problems/check-array-formation-through-concatenation

Solution

class Solution:
    def canFormArray(self, arr: List[int], pieces: List[List[int]]) -> bool:
        pieces_dict = {p[0]: p for p in pieces}

        i = 0
        while i < len(arr):
            p = pieces_dict.get(arr[i])

            if not p:
                return False

            for j in range(len(p)):
                if i >= len(arr) or arr[i] != p[j]:
                    return False

                i += 1

        return True