package leetcode;

/**
 * https://leetcode.com/problems/score-of-parentheses/
 */
public class Problem856 {
    public int scoreOfParentheses(String S) {
        return scoreOfParentheses(S, 0);
    }

    private int scoreOfParentheses(String s, int i) {
        if (i >= s.length()) {
            return 0;
        }
        int result = 0;
        if (s.charAt(i) == '(' && s.charAt(i + 1) == ')') {
            result = 1 + scoreOfParentheses(s, i + 2);
        } else if (s.charAt(i) == '(') {
            result = 2 * scoreOfParentheses(s, i + 1);
        } else {
//            result = scoreOfParentheses(s, i + 1);
        }
        return result;
    }

    public static void main(String[] args) {
        Problem856 prob = new Problem856();
//        System.out.println(prob.scoreOfParentheses("()")); // 1
//        System.out.println(prob.scoreOfParentheses("(())")); // 2
//        System.out.println(prob.scoreOfParentheses("()()()")); // 3
//        System.out.println(prob.scoreOfParentheses("(())()")); // 3
        System.out.println(prob.scoreOfParentheses("(()())()")); // 5
//        System.out.println(prob.scoreOfParentheses("()()")); // 2
//        System.out.println(prob.scoreOfParentheses("(()(()))")); // 6
//        System.out.println(prob.scoreOfParentheses("((()))")); // 4
//        System.out.println(prob.scoreOfParentheses("((()()))")); // 8
//        System.out.println(prob.scoreOfParentheses("((()())())")); // 10
    }
}

@@ -184,17 +184,28 @@
 eses(S, 
-0
+new IntRef()
 );%0A    %7D
@@ -202,24 +202,92 @@
 ());%0A    %7D%0A%0A
+    private static class IntRef %7B%0A        private int index;%0A    %7D%0A%0A
     private 
@@ -319,21 +319,26 @@
 ring s, 
-int i
+IntRef ref
 ) %7B%0A    
@@ -346,86 +346,68 @@
    i
-f (i %3E= s.length()) %7B%0A            return 0;%0A        %7D%0A        int result = 0;%0A
+nt result = 0;%0A        while (ref.index %3C s.length()) %7B%0A    
     
@@ -415,33 +415,41 @@
    if (s.charAt(
-i
+ref.index
 ) == '(' && s.ch
@@ -453,17 +453,25 @@
 .charAt(
-i
+ref.index
  + 1) ==
@@ -494,51 +494,58 @@
     
+    
 result
- = 1 + scoreOfParentheses(s, i
+++;%0A                ref.index
  +
+=
  2
-)
 ;%0A
+    
     
@@ -568,17 +568,25 @@
 .charAt(
-i
+ref.index
 ) == '('
@@ -605,70 +605,29 @@
     
-result = 2 * scoreOfParentheses(s, i + 1);%0A        %7D else %7B%0A//
+    ref.index++;%0A    
     
@@ -641,17 +641,22 @@
  result 
-=
++= 2 *
  scoreOf
@@ -674,21 +674,19 @@
 (s, 
-i + 1
+ref
 );%0A     
    %7D
@@ -685,832 +685,130 @@
     
-   %7D%0A
        
- return result;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem856 prob = new Problem856();%0A//        System.out.println(prob.scoreOfParentheses(%22()%22)); // 1%0A//        System.out.println(prob.scoreOfParentheses(%22(())%22)); // 2%0A//        System.out.println(prob.scoreOfParentheses(%22()()()%22)); // 3%0A//        System.out.println(prob.scoreOfParentheses(%22(())()%22)); // 3%0A        System.out.println(prob.scoreOfParentheses(%22(()())()%22)); // 5%0A//        System.out.println(prob.scoreOfParentheses(%22()()%22)); // 2%0A//        System.out.println(prob.scoreOfParentheses(%22(()(()))%22)); // 6%0A//        System.out.println(prob.scoreOfParentheses(%22((()))%22)); // 4%0A//        System.out.println(prob.scoreOfParentheses(%22((()()))%22)); // 8%0A//        System.out.println(prob.scoreOfParentheses(%22((()())())%22)); // 10
+%7D else %7B%0A                ref.index++;%0A                return result;%0A            %7D%0A        %7D%0A        return result;
 %0A   

Solve problem 856