import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param s string字符串
     * @return bool布尔型
     */
    static public boolean isValidString(String s) {
        if (s == null || s.isEmpty()) {
            return true;
        }
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '(') {
                stack.push('(');
            }
            if (s.charAt(i) == ')') {
                if (stack.isEmpty()) {
                    return false;
                }
                int count = 0;
                Character pop = stack.pop();
                while (pop == '*') {
                    count++;
                    if (stack.isEmpty()) {
                        break;
                    }
                    pop = stack.pop();
                }
                if (count == 0 && pop != '(') {
                    return false;
                }
                if (pop != '(') {
                    for (int j = 1; j < count; j++) {
                        stack.push('*');
                    }
                } else {
                    for (int j = 1; j <= count; j++) {
                        stack.push('*');
                    }
                }
            }
            if (s.charAt(i) == '*') {
                stack.push('*');
            }
        }

        int start = 0;
        while (!stack.isEmpty()) {
            Character pop = stack.pop();
            if (pop == '(') {
                start--;
                if (start < 0) {
                    return false;
                }
            } else if (pop == '*') {
                start++;
            }
        }
        return true;
    }
}