-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathB.java
More file actions
31 lines (27 loc) · 819 Bytes
/
Copy pathB.java
File metadata and controls
31 lines (27 loc) · 819 Bytes
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
package foobar.level_2;
@SuppressWarnings("UtilityClassWithoutPrivateConstructor")
public final class B {
public static void main(String[] args) {
System.out.println(solution(">----<"));
System.out.println(solution("<<>><"));
System.out.println(solution("--->-><-><-->-"));
}
public static int solution(String s) {
final char[] w = s.toCharArray();
int res = 0;
final int[] opposite = new int[w.length];
int curr = 0;
for (int i = w.length - 1; i >= 0; i--) {
if (w[i] == '<') {
curr++;
}
opposite[i] = curr;
}
for (int i = 0; i < w.length; i++) {
if (w[i] == '>') {
res += opposite[i];
}
}
return res * 2;
}
}