-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConsecutiveAppearances.java
More file actions
45 lines (41 loc) · 1.19 KB
/
ConsecutiveAppearances.java
File metadata and controls
45 lines (41 loc) · 1.19 KB
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
35
36
37
38
39
40
41
42
43
44
45
public class ConsecutiveAppearances {
public static void main(String[] args) {
int[] A = {55, 33, 22};
int[] B = {7, 6, 55, 33, 22, 67, 9};
System.out.println(appearsConsecutively(A, B));
}
// public static boolean appearsConsecutively(int[] A, int[] B) {
// int count = 0, countB = 0;
// boolean consecutive = false;
//
// while(count < A.length && countB < B.length) {
// if(A[count] == B[countB]) {
// count++;
// countB++;
// } else {
// count = 0;
// countB++;
// }
// }
// if(count == A.length) {
// consecutive = true;
// }
// return consecutive;
// }
public static boolean appearsConsecutively(int[] A, int[] B) {
int count = 0;
boolean found = false;
for (int i = 0; i < B.length; i++) {
if (A[count] == B[i]) {
if (count == A.length) {
found = true;
} else {
count++;
}
} else {
count = 0;
}
}
return found;
}
}