![]() |
VOOZH | about |
Given a string S of length N. The string consists only of letters 'F' and 'B'. The task is to generate a sequence performing some operations such that:
Examples :
Input: N = 5, S = "FBBFB"
Output: 1 2 4 5 3 0
Explanation: Initially, A = {0}.
S[1] is 'F' , sequence becomes {1, 0}
S[2] is 'B' , sequence becomes {1, 2, 0}
S[3] is 'B' , sequence becomes {1, 2, 3, 0}
S[4] is 'F' , sequence becomes {1, 2, 4, 3, 0}
S[5] is 'B' , sequence becomes {1, 2, 4, 5, 3, 0}Input : N = 6 , S = "BBBBBB"
Output : 0 1 2 3 4 5 6
Approach: The idea to solve the problem is based on the concept dequeue.
As at each iteration, it is possible that insertion of i may occur from any end of (i-1), therefore deque can be used as in dequeue insertion is possible from any end.
Follow the steps for the solution:
Below is the implementation of the above approach.
1 2 4 5 3 0
Time Complexity: O(N)
Auxiliary Space: O(N)