package leetcode;

import java.util.Stack;

/**
 * https://leetcode.com/problems/valid-parenthesis-string/
 */
public class Problem678 {
    public boolean checkValidString(String s) {
        if (s.length() % 2 != 0) {
            return false;
        }
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '(' || s.charAt(i) == '*') {
                stack.push(s.charAt(i));
            } else if (s.charAt(i) == ')') {
                if (stack.peek() == '(' || stack.peek() == '*') {
                    stack.pop();
                } else {
                    return false;
                }
            }
        }
        if (stack.size() % 2 != 0) {
            return false;
        }
        while (!stack.isEmpty()) {
            char a = stack.pop();
            char b = stack.pop();
            if (a == '(' || b == ')') {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Problem678 prob = new Problem678();
        System.out.println(prob.checkValidString("()")); // true
        System.out.println(prob.checkValidString("(*)")); // true
//        System.out.println(prob.checkValidString("(*))")); // true
//        System.out.println(prob.checkValidString(")(")); // false
//        System.out.println(prob.checkValidString("()()")); // true
//        System.out.println(prob.checkValidString("(**())")); // true
    }
}

@@ -16,33 +16,8 @@
 e;%0A%0A
-import java.util.Stack;%0A%0A
 /**%0A
@@ -165,162 +165,101 @@
     
-if (s.length() %25 2 != 0) %7B%0A            return false;%0A        %7D%0A        Stack%3CCharacter%3E stack = new Stack%3C%3E();%0A        for (int i = 0; i %3C s.length(); i++
+return check(s, 0, 0);%0A    %7D%0A%0A    private static boolean check(String s, int idx, int opening
 ) %7B%0A
@@ -270,142 +270,92 @@
     
- 
-   
 if (
-s.charAt(i) == '(' %7C%7C s.charAt(i) == '*') %7B%0A                stack.push(s.charAt(i));%0A            %7D else if (s.charAt(i
+opening %3C 0) %7B%0A            return false;%0A        %7D%0A        if (s.length(
 ) == 
-')'
+idx
 ) %7B%0A
@@ -370,128 +370,71 @@
     
- 
-   
 if (
-stack.peek() == '(' %7C%7C stack.peek() == '*') %7B%0A                    stack.pop();%0A                %7D else %7B%0A        
+opening == 0) %7B%0A                return true;%0A            %7D%0A
     
@@ -459,32 +459,34 @@
 se;%0A        
+%7D%0A
         
 %7D%0A          
@@ -477,324 +477,422 @@
     
-%7D%0A            %7D%0A        %7D%0A        if (stack.size() %25 2 != 0) %7B%0A            return false;%0A        %7D%0A        while (!stack.isEmpty()) %7B%0A            char a = stack.pop();%0A            char b = stack.pop();%0A            if (a == '(' %7C%7C b == ')') %7B%0A                return false;%0A            %7D%0A        %7D%0A        return true;
+if (s.charAt(idx) == '(') %7B%0A            return check(s, idx + 1, opening + 1);%0A        %7D else if (s.charAt(idx) == ')') %7B%0A            return check(s, idx + 1, opening - 1);%0A        %7D else %7B%0A            boolean a = check(s, idx + 1, opening + 1); // (%0A            boolean b = check(s, idx + 1, opening); // )%0A            boolean c = check(s, idx + 1, opening - 1); // empty%0A            return a %7C%7C b %7C%7C c;%0A        %7D
 %0A   
@@ -1107,34 +1107,32 @@
 (*)%22)); // true%0A
-//
         System.o
@@ -1174,34 +1174,32 @@
 *))%22)); // true%0A
-//
         System.o
@@ -1248,18 +1248,16 @@
 / false%0A
-//
         
@@ -1315,18 +1315,16 @@
 // true%0A
-//
         

Update problem 678