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

Return true if it is possible to form the array arr from pieces. Otherwise, return false.

&nbsp;
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]


&nbsp;
Constraints:


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


class Solution:
    def canFormArray(self, arr: List[int], pieces: List[List[int]]) -> bool:
        keys, ans = {}, []
        for piece in pieces:
            keys[piece[0]] = piece
        for a in arr:
            if a in keys:
                ans.extend(keys[a])
        return ''.join(map(str, arr)) == ''.join(map(str, ans))
class Solution {
    public boolean canFormArray(int[] arr, int[][] pieces) {
        HashMap<Integer,int[]> hm = new HashMap();
        for(int[] list:pieces)
            hm.put(list[0],list);
        
        int index = 0;
        while(index<arr.length){
            if(!hm.containsKey(arr[index]))
                return false;
            
            int[] list = hm.get(arr[index]);
            for(int val:list){
                if(index>=arr.length || val!=arr[index])
                    return false;
                index++;
            }
        }
        return true;
    }
}
class Solution {
public:
    bool canFormArray(vector<int>& arr, vector<vector<int>>& pieces) {
        map<int,vector<int>> mp; 
        // map the 1st element in pieces[i] to pieces[i]
        for(auto p:pieces) 
            mp[p[0]] = p;
        vector<int> result;
        for(auto a:arr) {
            if(mp.find(a)!=mp.end()) 
                result.insert(result.end(),mp[a].begin(),mp[a].end());
        }
        return result ==arr;
    }
};
var canFormArray = function(arr, pieces) {
	let total = "";
    arr=arr.join("");
    for (let i = 0; i < pieces.length; i++) {
      pieces[i] = pieces[i].join("");
      total += pieces[i];
      if (arr.indexOf(pieces[i]) == -1) return false;
    }
    return total.length == arr.length;
};
Check Array Formation Through Concatenation