forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1640.java
34 lines (32 loc) · 996 Bytes
/
_1640.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.fishercoder.solutions;
public class _1640 {
public static class Solution1 {
public boolean canFormArray(int[] arr, int[][] pieces) {
for (int[] piece : pieces) {
int first = piece[0];
int index = findIndex(arr, first);
if (index == -1) {
return false;
}
int i = 0;
for (int j = index; i < piece.length && j < arr.length; i++, j++) {
if (arr[j] != piece[i]) {
return false;
}
}
if (i != piece.length) {
return false;
}
}
return true;
}
private int findIndex(int[] arr, int key) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == key) {
return i;
}
}
return -1;
}
}
}