Java solution - no reverse string - beats 100%


  • 0
    L
    public int[] findPermutation(String s) {
        if(s == null || s.length() == 0) return new int[0];
        int minNumber = 1;
        int n = s.length();
        int[] ans = new int[n + 1];
        
        for(int i = 0; i < s.length(); i++){
            char c = s.charAt(i);
            if(c == 'I'){
                ans[i] = minNumber++;
            } else {
                int count = 1;
                while((i+count) < s.length() && s.charAt(i+count) == 'D'){
                    count++;
                }
                for(int j = i+count; j >= i; j--){
                    ans[j] = minNumber++;
                }
                i += count;
            }
        }
        
        if(ans[right-1] == 0)
            ans[right-1] = minNumber;
        return ans;
    }

Log in to reply
 

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