Check Array Formation Through Concatenation | Java
Explanation:\nStep 1:\nCreate a HashMap. Since we cannot change the order of the pieces, we are concerned only about the first element of every piece. So the ke
**Explanation**:\n**Step 1:**\nCreate a HashMap. Since we cannot change the order of the pieces, we are concerned only about the first element of every piece. So the key for the hashmap will be the first element of every piece, and the value would be the array itself. \n\n**Step 2:**\nIterate through the ***arr*** array. If the HashMap does not have any of the arr elements as its key, it means we cannot concatenate. Return False. If an element is matched with the Key, iterate through the array associated with the key. \n\n```\nclass Solution {\n    public boolean canFormArray(int[] arr, int[][] pieces) {\n        HashMap <Integer, int []> hashMap = new HashMap <>();\n        \n        for (int [] piece : pieces) {\n            hashMap.put (piece [0], piece);\n        }\n        \n        int index = 0;\n        while (index < arr.length) {\n            if (hashMap.containsKey (arr [index])) {\n                int [] temp = hashMap.get (arr [index]);\n                for (int inIndex = 0; inIndex < temp.length; inIndex++) {\n                    if (temp [inIndex] != arr [index]) {\n                        return false;\n                    }\n                    index++;\n                }\n            }\n            else {\n                return false;\n            }\n        }\n        return true;\n    }\n}\n```\n\nTime Complexity: O (n + s)\nn = Number of elements in arr\ns = Number of elements in Pieces
[]