Skip to content

Instantly share code, notes, and snippets.

@ianomad
Last active June 24, 2018 03:14
Show Gist options
  • Save ianomad/2bffdbff73f93965ad237a87a489bc15 to your computer and use it in GitHub Desktop.
Save ianomad/2bffdbff73f93965ad237a87a489bc15 to your computer and use it in GitHub Desktop.
class Solution {
private static final int CONTAINER = 1;
private static final int BOX = 0;
public int scoreOfParentheses(String S) {
Stack<Integer> data = new Stack<Integer>();
int sum = 0;
for (int i = 0; i < S.length(); i++) {
char c = S.charAt(i);
if (c == '(') {
data.push(S.charAt(i + 1) == ')' ? BOX : CONTAINER);
} else {
int type = data.pop();
if (type == BOX) {
sum += Math.pow(2, data.size());
}
}
}
return sum;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment