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:
- Consider an integer sequence A that consists of only a 0, i.e. A = (0).
- Now, for each index(i) of the string (1 to N), if S[i] is ‘F’ add i to the immediate front (i.e. left side) of i-1 in sequence A
- Else if S[i] is ‘B’ add i to the immediate back (i.e. right side) of i-1 sequence A.
- Print the resultant sequence A.
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:
- The observation in the approach is that the problem becomes more simple after inverting all the operations.
- The problem now modifies to, a given sequence that consists of only (N), and after each iteration from the end of the string,
- If S[i] is ‘F’, push i to the right of i-1,
- If S[i] is ‘B’, push i to the left of i-1 in the dequeue.
- Return the elements of dequeue in the order from the front end.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void findSequence( int N, string S)
{
deque< int > v;
v.push_back(N);
for ( int i = N - 1; i >= 0; i--) {
if (S[i] == 'F' ) {
v.push_back(i);
}
else {
v.push_front(i);
}
}
for ( int i = 0; i <= N; i++)
cout << v[i] << " " ;
}
int main()
{
int N = 5;
string S = "FBBFB" ;
findSequence(N, S);
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static void findSequence( int N, String S)
{
Deque<Integer> v = new ArrayDeque<Integer>();
v.addLast(N);
for ( int i = N - 1 ; i >= 0 ; i--) {
if (S.charAt(i) == 'F' ) {
v.addLast(i);
}
else {
v.addFirst(i);
}
}
for (Iterator itr = v.iterator(); itr.hasNext();) {
System.out.print(itr.next() + " " );
}
}
public static void main(String[] args)
{
int N = 5 ;
String S = "FBBFB" ;
findSequence(N, S);
}
}
|
Python3
from collections import deque
def findSequence(N, S):
v = deque()
v.append(N)
i = N - 1
while (i > = 0 ):
if (S[i] = = 'F' ):
v.append(i)
else :
v.appendleft(i)
i - = 1
print ( * v)
N = 5
S = "FBBFB"
findSequence(N, S)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
public static void findSequence( int N, String S)
{
List< int > v = new List< int >();
v.Add(N);
for ( int i = N - 1; i >= 0; i--) {
if (S[i] == 'F' ) {
v.Add(i);
}
else {
v.Insert(0,i);
}
}
foreach ( int itr in v) {
Console.Write(itr + " " );
}
}
public static void Main(String[] args)
{
int N = 5;
String S = "FBBFB" ;
findSequence(N, S);
}
}
|
Javascript
<script>
const findSequence = (N, S) => {
let v = [];
v.push(N);
for (let i = N - 1; i >= 0; i--) {
if (S[i] == 'F' ) {
v.push(i);
}
else {
v.unshift(i);
}
}
for (let i = 0; i <= N; i++)
document.write(`${v[i]} `);
}
let N = 5;
let S = "FBBFB" ;
findSequence(N, S);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)