<p>You are given an array of <strong>distinct</strong> integers <code>arr</code> and an array of integer arrays <code>pieces</code>, where the integers in <code>pieces</code> are <strong>distinct</strong>. Your goal is to form <code>arr</code> by concatenating the arrays in <code>pieces</code> <strong>in any order</strong>. However, you are <strong>not</strong> allowed to reorder the integers in each array <code>pieces[i]</code>.</p>

<p>Return <code>true</code> <em>if it is possible </em><em>to form the array </em><code>arr</code><em> from </em><code>pieces</code>. Otherwise, return <code>false</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> arr = [15,88], pieces = [[88],[15]]
<strong>Output:</strong> true
<strong>Explanation:</strong> Concatenate [15] then [88]
</pre>

<p><strong class="example">Example 2:</strong></p>

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

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> arr = [91,4,64,78], pieces = [[78],[4,64],[91]]
<strong>Output:</strong> true
<strong>Explanation:</strong> Concatenate [91] then [4,64] then [78]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= pieces.length &lt;= arr.length &lt;= 100</code></li>
	<li><code>sum(pieces[i].length) == arr.length</code></li>
	<li><code>1 &lt;= pieces[i].length &lt;= arr.length</code></li>
	<li><code>1 &lt;= arr[i], pieces[i][j] &lt;= 100</code></li>
	<li>The integers in <code>arr</code> are <strong>distinct</strong>.</li>
	<li>The integers in <code>pieces</code> are <strong>distinct</strong> (i.e., If we flatten pieces in a 1D array, all the integers in this array are distinct).</li>
</ul>

from typing import List

class Solution:
    def canFormArray(self, arr: List[int], pieces: List[List[int]]) -> bool:
        # Create a dictionary to map the first element of each piece to the piece itself
        pieces_dict = {piece[0]: piece for piece in pieces}
        
        # Initialize a pointer to track the current position in the arr list
        i = 0
        
        # Loop until we reach the end of the arr list
        while i < len(arr):
            # If the current element in arr is not the start of any piece, return False
            if arr[i] not in pieces_dict:
                return False
            
            # Get the piece that starts with the current element
            piece = pieces_dict[arr[i]]
            
            # Check if the piece matches the next elements in arr
            for j in range(len(piece)):
                # If the elements don't match, return False
                if arr[i + j] != piece[j]:
                    return False
            
            # Move the pointer by the length of the piece
            i += len(piece)
        
        # If we successfully iterate through the entire arr list, return True
        return True

# Example usage:
solution = Solution()
arr = [15, 88]
pieces = [[88], [15]]
print(solution.canFormArray(arr, pieces))  # Output: True

arr = [49, 18, 16]
pieces = [[16, 18, 49]]
print(solution.canFormArray(arr, pieces))  # Output: False

arr = [91, 4, 64, 78]
pieces = [[78], [4, 64], [91]]
print(solution.canFormArray(arr, pieces))  # Output: True
35