Given a balanced parentheses string s, return the score of the string.

The score of a balanced parentheses string is based on the following rule:


	"()" has score 1.
	AB has score A + B, where A and B are balanced parentheses strings.
	(A) has score 2 * A, where A is a balanced parentheses string.


&nbsp;
Example 1:

Input: s = "()"
Output: 1


Example 2:

Input: s = "(())"
Output: 2


Example 3:

Input: s = "()()"
Output: 2


&nbsp;
Constraints:


	2 &lt;= s.length &lt;= 50
	s consists of only '(' and ')'.
	s is a balanced parentheses string.


class Solution:
    def scoreOfParentheses(self, s: str) -> int:
        
        level = 0
        result = 0
        prev = ""
        
        for c in s:           
            if c == "(":
                level += 1
            if c == ")":
                if prev == "(":
                    result += 2 ** (level - 1)
                level -= 1                
            prev = c
            
        return result
class Solution {
    public int scoreOfParentheses(String s) {
        Stack<Integer> st = new Stack<>();
        int score = 0;
        for(int i = 0; i < s.length(); i++){
            char ch = s.charAt(i);
            if(ch == '('){
                st.push(score);
                score = 0;
            }
            else {
                score = st.pop() + Math.max(2 * score, 1);
            }
        }
        return score;
    }
}
class Solution {
public:
    int scoreOfParentheses(string s) {
        stack<int> st;
        int score = 0;
        for(int i = 0; i < s.size(); i++){
            if(s[i] == '('){
                st.push(score);
                score = 0;
            }
            else {
                score = st.top() + max(2 * score, 1);
                st.pop();
            }
        }
        return score;
    }
};
var scoreOfParentheses = function(s) {
    let len = s.length, pwr = 0, ans = 0;

    for (let i = 1; i < len; i++){
        if (s.charAt(i) === "("){
            pwr++;
        }
        else if (s.charAt(i-1) === "("){
            ans += 1 << pwr--;
        }
        else{
            pwr--;
        }
    }

    return ans;
}
Score of Parentheses