Form Array by Concatenating Subarrays of Another Array LeetCode solution in Java

1
class Solution {
2

3
public int search(int[] group, int[] nums, int start, int end) {
4
int i = start, j = 0;
5
while (i < end && j < group.length) {
6
if (nums[i] == group[j]) {
7
i++;
8
j++;
9

10
if (j == group.length) return i;
11
} else {
12
i = i - j + 1;
13
j = 0;
14
}
15
}
16
return -1;
17
}
18

19
public boolean canChoose(int[][] groups, int[] nums) {
20
int start = 0, end = nums.length;
21

22
for (int[] group : groups) {
23
start = search(group, nums, start, end);
24
if (start == -1) return false;
25
}
26

27
return true;
28
}
29
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0