Skip to content

Latest commit

 

History

History
23 lines (21 loc) · 620 Bytes

1640.md

File metadata and controls

23 lines (21 loc) · 620 Bytes

1640. Check Array Formation Through Concatenation

Solution 1 (time O(n), space O(n))

class Solution(object):
    def canFormArray(self, arr, pieces):
        """
        :type arr: List[int]
        :type pieces: List[List[int]]
        :rtype: bool
        """
        index = {p[0]: i for i, p in enumerate(pieces)}
        i = 0
        while i < len(arr):
            if arr[i] not in index:
                return False
            p = pieces[index[arr[i]]]
            if arr[i: i + len(p)] != p:
                return False
            i += len(p)
        return True