package leetcode;

/**
 * https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/
 */
public class Problem921 {
    public int minAddToMakeValid(String S) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem921 prob = new Problem921();
        System.out.println(prob.minAddToMakeValid("())")); // 1
        System.out.println(prob.minAddToMakeValid("(((")); // 3
        System.out.println(prob.minAddToMakeValid("()")); // 0
        System.out.println(prob.minAddToMakeValid("()))((")); // 4
    }
}

@@ -12,16 +12,41 @@
 tcode;%0A%0A
+import java.util.Stack;%0A%0A
 /**%0A * h
@@ -200,387 +200,475 @@
     
-// TODO%0A        return 0
+int count = 0;%0A        Stack%3CCharacter%3E stack = new Stack%3C%3E()
 ;%0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem921 prob = new Problem921();%0A        System.out.println(prob.minAddToMakeValid(%22())%22)); // 1%0A        System.out.println(prob.minAddToMakeValid(%22(((%22)); // 3%0A        System.out.println(prob.minAddToMakeValid(%22()%22)); // 0%0A        System.out.println(prob.minAddToMakeValid(%22()))((%22)); // 4
+for (int i = 0; i %3C S.length(); i++) %7B%0A            char c = S.charAt(i);%0A            if (c == '(') %7B%0A                stack.add('(');%0A            %7D else if (c == ')') %7B%0A                if (!stack.isEmpty() && stack.peek() == '(') %7B%0A                    stack.pop();%0A                %7D else %7B%0A                    count++;%0A                %7D%0A            %7D%0A        %7D%0A        return count + stack.size();
 %0A   

Solve problem 921