-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution856.java
More file actions
30 lines (29 loc) · 801 Bytes
/
Copy pathsolution856.java
File metadata and controls
30 lines (29 loc) · 801 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
import java.util.Stack;
public class solution856 {
public int scoreOfParentheses(String S) {
Stack<Integer> stack = new Stack<>();
int sum = 0;
for(int i = 0;i<S.length();i++){
int res = 0;
char c = S.charAt(i);
if(c=='('){
stack.push(-1);
}else{
if(stack.peek()==-1){
stack.pop();
stack.push(1);
}else{
while(stack.peek()!=-1){
res=res+stack.pop();
}
stack.pop();
stack.push(2*res);
}
}
}
while(!stack.isEmpty()){
sum = sum+stack.pop();
}
return sum;
}
}