Java 0(n) solution easy


#1
public ArrayList<Integer> findPerm(final String A, int B) {
    ArrayList<Integer> ans = new ArrayList<Integer>();
    Stack<Integer> stk = new Stack<Integer>(); 

    // run n+1 times where n is length of input sequence 
    for (int i = 0; i <= A.length(); i++) { 
        // push number i+1 into the stack 
        stk.push(i + 1); 

        // if all characters of the input sequence are 
        // processed or current character is 'I' 
        // (increasing) 
        if (i == A.length() || A.charAt(i) == 'I') { 
            // run till stack is empty 
            while (!stk.empty()) { 
                // remove top element from the stack and 
                // add it to solution 
                ans.add(stk.peek());
                stk.pop(); 
            } 
        } 
    } 
    return ans;
}