Simple C++ solution with comments


#1
vector<int> Solution::findPerm(const string A, int B) {
    int cntI=0; //stores the count of I in the string A
    for(int i=0; i<B-1; i++)    {
        if(A[i]=='I')   cntI++;
    }
    int cntD = A.size()-cntI; //stores the count of D in the string A
    int down=cntD, up=B+1-cntI; //'down' will put the value where D is in the string A and up will put the value in place of I.
    vector<int> v(B);
    for(int i=0; i<B-1; i++)    {
        if(A[i]=='D')   v[i+1] = down--;
        else    v[i+1] = up++;
    }
    //Now the all places in the vector are filled except the first element. So, we give value to v[0];
    v[0] = cntD+1;
    return v;
}