Simplest and Shortest code, O(n) time & O(1) space


#1

I suggest if you stuck in this problem then look at hint -1 and then try to solve. But somehow if you are unable to solve this problem then see the code here.

vector<int> Solution::findPerm(const string A, int B) {
    vector<int> v;
    int l = 1, r = B;
    for(int i = 0; i<B-1; i++)
    {
        if(A[i]=='I')
            v.push_back(l++);
        else
            v.push_back(r--);
    }
    v.push_back(l);
    return v;
}