-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColouredTriangles.java
More file actions
43 lines (33 loc) · 1.1 KB
/
Copy pathColouredTriangles.java
File metadata and controls
43 lines (33 loc) · 1.1 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
package kyu7;
import java.util.ArrayList;
public class ColouredTriangles {
public static char triangle(final String row) {
if (row.length() == 1) {
return row.charAt(0);
}
final ArrayList<Character> characters = new ArrayList<>();
for (int i = 0; i < row.length() - 1; i++) {
char colour = getColour(row.charAt(i), row.charAt(i + 1));
for (int j = 0; j < characters.size(); j++) {
char prevColour = characters.get(j);
characters.set(j, colour);
colour = getColour(prevColour, colour);
}
characters.add(colour);
}
return characters.get(characters.size() - 1);
}
private static char getColour(char a, char b) {
if (a == b) {
return a;
} else {
if ((a == 'R' && b == 'B') || (a == 'B' && b == 'R')) {
return 'G';
} else if ((a == 'G' && b == 'B') || a == 'B' && b == 'G') {
return 'R';
} else {
return 'B';
}
}
}
}