# 通过串联Leetcode解决方案检查阵列形成

`arr = [15,88], pieces = [[88],[15]]`
`true`

`arr = [91,4,64,78], pieces = [[78],[4,64],[91]]`
`true`

## 通过级联Leetcode解决方案形成检查阵列的代码

### C ++代码

```#include <bits/stdc++.h>
using namespace std;

bool canFormArray(vector<int>& arr, vector<vector<int>>& pieces) {
unordered_map<int,int> entry;
for(int i=0;i<pieces.size();i++)
entry[pieces[i][0]] = i;
int i =  0;
while(i < arr.size()){
if(entry.count(arr[i])){
vector<int> &piece  = pieces[entry[arr[i]]];
for(auto x: piece)
if(x != arr[i++])
return false;
} else {
return false;
}
}
return true;
}

int main(){
vector<int> arr = {91, 4, 64, 78};
vector<vector<int>> pieces = {{78},{4,64},{91}};
cout<<(canFormArray(arr, pieces) ? "true" : "false");
}
```
```true
```

### Java代码

```import java.util.*;
import java.lang.*;
import java.io.*;

class Main
{
public static boolean canFormArray(int[] arr, int[][] pieces) {
HashMap<Integer, Integer> entry = new HashMap<Integer, Integer>();
for(int i=0;i<pieces.length;i++)
entry.put(pieces[i][0], i);
int i =  0;
while(i < arr.length){
if(entry.containsKey(arr[i])){
int n = pieces[entry.get(arr[i])].length;
int k = entry.get(arr[i]);
for(int j=0;j<n;j++)
if(pieces[k][j] != arr[i++])
return false;
} else {
return false;
}
}
return true;
}

public static void main (String[] args) throws java.lang.Exception {
int[] arr = {91, 4, 64, 78};
int[][] pieces = {{78},{4,64},{91}};
System.out.print(canFormArray(arr, pieces) ? "true" : "false");
}
}```
`true`

## 复杂度分析

### 空间复杂度

O（M）， 其中，M是数组数组中的数组数。