Skip to content

Commit 1aea0b5

Browse files
authored
Create check-array-formation-through-concatenation.cpp
1 parent 1b72293 commit 1aea0b5

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
bool canFormArray(vector<int>& arr, vector<vector<int>>& pieces) {
7+
unordered_map<int, int> lookup;
8+
for (int i = 0; i < size(pieces); ++i) {
9+
lookup[pieces[i][0]] = i;
10+
}
11+
for (int i = 0; i < size(arr);) {
12+
if (!lookup.count(arr[i])) {
13+
return false;
14+
}
15+
for (const auto& c : pieces[lookup[arr[i]]]) {
16+
if (i == size(arr) || arr[i] != c) {
17+
return false;
18+
}
19+
++i;
20+
}
21+
}
22+
return true;
23+
}
24+
};

0 commit comments

Comments
 (0)