Last active
June 24, 2018 03:14
-
-
Save ianomad/2bffdbff73f93965ad237a87a489bc15 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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