-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
35 lines (27 loc) · 924 Bytes
/
Solution.java
File metadata and controls
35 lines (27 loc) · 924 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
31
32
33
34
35
import java.util.Stack;
public class Solution {
public static void main(String[] args){
String arrangement = "()(((()())(())()))(())";
int answer = solution(arrangement);
System.out.println(answer);
}
public static int solution(String arrangement) {
int answer = 0;
Stack<Integer> stack = new Stack<Integer>();
String arr = arrangement.replace("()", "0");
for(int i=0; i<arr.length(); i++){
if(arr.charAt(i) == '0') {
// laser case
if(!stack.isEmpty()) answer += stack.size();
} else if(arr.charAt(i) == '('){
// start of stick
stack.push(i);
} else if(arr.charAt(i) == ')') {
// end of stick
stack.pop();
answer++;
}
}
return answer;
}
}