Java O(n2) memory search


  • 0
    H

    Draw the search tree and you'll find many subtree repeated:

    class Solution {
        public boolean checkValidString(String s) {
            if(s==null) return false;
            if(s.length()==0)return true;
            return dfs(0, 0, s.toCharArray(), new int[s.length()][s.length()+1]);
        }
        private boolean dfs(int idx, int cnt, char[] sArr, int[][] memo){
            if(cnt < 0) return false;
            if(idx >= sArr.length){
                return cnt==0;
            }
            if(memo[idx][cnt] != 0){
                return memo[idx][cnt]==2; // 2 means valid 1 invalid
            }
            boolean ret = false;
            if(sArr[idx]=='('){
                ret = dfs(idx+1, cnt+1, sArr, memo);
            }else if(sArr[idx]==')'){
                ret = dfs(idx+1, cnt-1, sArr, memo);
            }else{
                ret = dfs(idx+1, cnt, sArr, memo)
                    ||dfs(idx+1, cnt-1, sArr, memo)
                    ||dfs(idx+1, cnt+1, sArr, memo);
            }
            memo[idx][cnt] = ret?2:1;
            return ret;
        }
    }
    

Log in to reply
 

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