Skip to content

Commit 1953afe

Browse files
authored
Create check-array-formation-through-concatenation.py
1 parent 1aea0b5 commit 1953afe

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
class Solution(object):
5+
def canFormArray(self, arr, pieces):
6+
"""
7+
:type arr: List[int]
8+
:type pieces: List[List[int]]
9+
:rtype: bool
10+
"""
11+
lookup = {x[0]: i for i, x in enumerate(pieces)}
12+
i = 0
13+
while i < len(arr):
14+
if arr[i] not in lookup:
15+
return False
16+
for c in pieces[lookup[arr[i]]]:
17+
if i == len(arr) or arr[i] != c:
18+
return False
19+
i += 1
20+
return True

0 commit comments

Comments
 (0)