Simple O(N) O(1) solution


#1

vector Solution::findPerm(const string A, int B) {

vector<int>a(B,0);
for(int i=0;i<A.size();i++)
{
    if(A[i]=='I')a[i+1]=a[i]+1;
    else a[i+1]=a[i]-1;
}

int mi=*min_element(a.begin(),a.end());
if(mi<=0)
{
    for(int i=0;i<B;i++)
    {
        a[i]+=(abs(mi)+1);
    }
}
return a;

}