package leetcode;

/**
 * https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/
 */
public class Problem1249 {
    public String minRemoveToMakeValid(String s) {
        // TODO
        return null;
    }

    public static void main(String[] args) {
        Problem1249 prob = new Problem1249();
        System.out.println(prob.minRemoveToMakeValid("lee(t(c)o)de)")); //  "lee(t(c)o)de"
        System.out.println(prob.minRemoveToMakeValid("a)b(c)d")); //  "ab(c)d"
        System.out.println(prob.minRemoveToMakeValid("))((")); //  "ab(c)d"
        System.out.println(prob.minRemoveToMakeValid("(a(b(c)d)")); //  "a(b(c)d)"
    }
}

@@ -12,16 +12,89 @@
 tcode;%0A%0A
+import java.util.HashSet;%0Aimport java.util.Set;%0Aimport java.util.Stack;%0A%0A
 /**%0A * h
@@ -258,15 +258,45 @@
     
-// TODO
+Stack%3CInteger%3E stack = new Stack%3C%3E();
 %0A   
@@ -304,447 +304,701 @@
     
-return null
+Set%3CInteger%3E removed = new HashSet%3C%3E()
 ;%0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem1249 prob = new Problem1249();%0A        System.out.println(prob.minRemoveToMakeValid(%22lee(t(c)o)de)%22)); //  %22lee(t(c)o)de%22%0A        System.out.println(prob.minRemoveToMakeValid(%22a)b(c)d%22)); //  %22ab(c)d%22%0A        System.out.println(prob.minRemoveToMakeValid(%22))((%22)); //  %22ab(c)d%22%0A        System.out.println(prob.minRemoveToMakeValid(%22(a(b(c)d)%22)); //  %22a(b(c)d)%22
+for (int i = 0; i %3C s.length(); i++) %7B%0A            if (s.charAt(i) == '(') %7B%0A                stack.push(i);%0A            %7D else if (s.charAt(i) == ')') %7B%0A                if (!stack.isEmpty()) %7B%0A                    stack.pop();%0A                %7D else %7B%0A                    removed.add(i);%0A                %7D%0A            %7D%0A        %7D%0A        for (int index : stack) %7B%0A            removed.add(index);%0A        %7D%0A        StringBuilder answer = new StringBuilder();%0A        for (int i = 0; i %3C s.length(); i++) %7B%0A            if (!removed.contains(i)) %7B%0A                answer.append(s.charAt(i));%0A            %7D%0A        %7D%0A        return answer.toString();
 %0A   

Solve problem 1249