TLE Solution!


  • 0
    T
    class Solution {
        public int[] findPermutation(String s) {
            if(s==null || s.length()<1){
                return null;
            }
            
            boolean[] used = new boolean[s.length()+2];
            char[] array = s.toCharArray();
            
            int[] res = new int[s.length()+1];
            find(res,used,0,array);
            return res;
        }
        
        public boolean find(int[] res,boolean[] used,int index,char[] signature){
            if(index>=res.length){
                return true;
            }
            
            for(int i=1;i<=res.length;i++){
                if(used[i]==true){
                    continue;
                }
                if(index==0){
                    used[i]=true;
                    res[index]=i;
                    if(find(res,used,index+1,signature)){
                        return true;
                    }
                    used[i]=false;
                }else{
                   if(signature[index-1]=='I'&&i>res[index-1]){
                        used[i]=true;
                        res[index]=i;
                        if(find(res,used,index+1,signature)){
                            return true;
                        }
                        used[i]=false;
                   }else if(signature[index-1]=='D'&&i<res[index-1]){
                       res[index]=i;
                       used[i]=true;
                       if(find(res,used,index+1,signature)){
                           return true;
                       }
                       used[i]=false;
                   }
                }
            }
            
            return false;
        }
    }
    

Log in to reply
 

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