Different approach from the given solution


#1

vector Solution::findPerm(const string A, int B) {
vector X(B);
iota(X.begin(), X.end(), 1);
for(int i = 0; i < A.length(); i++) {
if(A[i] == ‘D’) {
int beg = i;
while(A[i] == ‘D’) i++;
int end = i;
reverse(X.begin() + beg, X.begin() + end + 1);
}
}
return X;
}

Algorithm:

Iterate over the string A:
if A[i] == ‘I’ -> ignore.
else if A[i] == ‘D’
Find all contiguous 'D’s from this point and reverse the sequence.

For instance:

DIDD
12345

DIDD
21345

DIDD
21345

DIDD
21543