1640. Check Array Formation Through Concatenation LeetCode Solution

In this guide, you will get 1640. Check Array Formation Through Concatenation LeetCode Solution with the best time and space complexity. The solution to Check Array Formation Through Concatenation problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Check Array Formation Through Concatenation solution in C++
  4. Check Array Formation Through Concatenation solution in Java
  5. Check Array Formation Through Concatenation solution in Python
  6. Additional Resources
1640. Check Array Formation Through Concatenation LeetCode Solution image

Problem Statement of Check Array Formation Through Concatenation

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.

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]

Constraints:

1 <= pieces.length <= arr.length <= 100
sum(pieces[i].length) == arr.length
1 <= pieces[i].length <= arr.length
1 <= arr[i], pieces[i][j] <= 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).

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

1640. Check Array Formation Through Concatenation LeetCode Solution in C++

class Solution {
 public:
  bool canFormArray(vector<int>& arr, vector<vector<int>>& pieces) {
    vector<int> concatenated;
    unordered_map<int, vector<int>> startToPiece;

    for (const vector<int>& piece : pieces)
      startToPiece[piece[0]] = piece;

    for (const int a : arr)
      if (startToPiece.contains(a))
        for (const int num : startToPiece[a])
          concatenated.push_back(num);

    return concatenated == arr;
  }
};
/* code provided by PROGIEZ */

1640. Check Array Formation Through Concatenation LeetCode Solution in Java

class Solution {
  public boolean canFormArray(int[] arr, int[][] pieces) {
    List<Integer> concatenated = new ArrayList<>();
    Map<Integer, int[]> startToPiece = new HashMap<>();

    for (int[] piece : pieces)
      startToPiece.put(piece[0], piece);

    for (final int a : arr)
      for (final int num : startToPiece.getOrDefault(a, new int[]))
        concatenated.add(num);

    return Arrays.equals(concatenated.stream().mapToInt(Integer::intValue).toArray(), arr);
  }
}
// code provided by PROGIEZ

1640. Check Array Formation Through Concatenation LeetCode Solution in Python

class Solution:
  def canFormArray(self, arr: list[int], pieces: list[list[int]]) -> bool:
    concatenated = []
    startToPiece = {piece[0]: piece for piece in pieces}

    for a in arr:
      concatenated += startToPiece.get(a, [])

    return concatenated == arr
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.