Easy Java Solution


  • 0
    Y
        public boolean checkValidString(String s) {
            return helper(s, 0, 0);
        }
        
        private boolean helper(String str, int index, int res){
            if(res < 0) return false;
            if(index == str.length() && res == 0) return true;
            if(index == str.length() && res != 0) return false;
            if(str.charAt(index) == '(')
                return helper(str, index + 1, res + 1);
            else if(str.charAt(index) == ')')
                return helper(str, index + 1, res - 1);
            else
                return helper(str, index + 1, res - 1) || helper(str, index + 1, res + 1) || helper(str, index + 1, res);
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.