Check Array Formation Through Concatenation LeetCode solution in Java

1
class Solution {
2
public boolean canFormArray(int[] arr, int[][] pieces) {
3
HashMap<Integer, int[]> hm = new HashMap();
4
for (int[] list : pieces) hm.put(list[0], list);
5

6
int index = 0;
7
while (index < arr.length) {
8
if (!hm.containsKey(arr[index])) return false;
9

10
int[] list = hm.get(arr[index]);
11
for (int val : list) {
12
if (index >= arr.length || val != arr[index]) return false;
13
index++;
14
}
15
}
16
return true;
17
}
18
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0