A simple Java solution in O(n) complexity


#1
public class Solution {

    public ArrayList<Integer> findPerm(final String A, int B) {
        int i=0;
        ArrayList<Integer> resp = new ArrayList<>();
        int l = 1;
        int r = B;
        for(i=0; i<A.length(); ++i) {
            if (A.charAt(i)=='I') resp.add(l++);
            if (A.charAt(i)=='D') resp.add(r--);
        }
        if (A.charAt(A.length()-1)=='I') resp.add(r);
        if (A.charAt(A.length()-1)=='D') resp.add(l);
        return resp;
    }
}