Given an array arr[] of length N consisting of uppercase English letters only and a letter ch. the task is to find the final array that will form by reversing the prefix each time the letter ch is found in the array.
Examples:
Input: arr[] = {‘A’, ‘B’, ‘X’, ‘C’, ‘D’, ‘X’, ‘F’}, ch= ‘X’
Output: D C X A B X F
Explanation:
First encounter of ‘X’ at index 2, Initial subarray = A, B, Final subarray = B, A, X.
Second encounter of ‘X’ at index 5, Initial subarray = B, A, X, C, D
Final subarray = D, C, X, A, B, X(added).
Final subarray after traversing, = D, C, X, A, B, X, F
Input: arr[] = {‘A’, ‘B’, ‘C’, ‘D’, ‘E’}, ch = ‘F’
Output: A B C D E
Approach: The idea to solve the problem is as follows:
If each portion between two occurrences of ch (or the ends of the array) is considered a segment, then the prefix reversals of the string can be visualised as appending the characters of a segment alternatively at the starting and the ending of string and keep expanding outwards.
- So idea is to push the element of the array into back of a list till ch occurs for first time.
- When first ch occurs, push the elements, including ch, to the front of the list till the next ch occurs. Again if ch occurs push the elements to the back of the list, including ch.
- So, the conclusion is that each time ch occurs, you have to change the direction of pushing the elements.
Note: If there is odd number of K in the array, you need to reverse the list as we start pushing element from back.
Follow The steps given below
- Create a list named li to store the elements
- Create a variable named found to check which side you have to add the elements from
- If ch occurs flip the value of found from 1 to 0 or 0 to 1.
- If found is equal to 1 add the elements to the front.
- Else add the elements to the back
- If ch occurs odd number of times reverse the list and print, else print it simply
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
list< char > findFinalString( char * arr, int n, char ch)
{
list< char > li;
bool found = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] == ch)
found = !found;
if (found)
li.push_front(arr[i]);
else
li.push_back(arr[i]);
}
if (found == 1)
li.reverse();
return li;
}
int main()
{
char arr[] = { 'A' , 'B' , 'X' , 'C' , 'D' , 'X' , 'F' };
char ch = 'X' ;
int N = sizeof (arr) / sizeof (arr[0]);
list< char > ans = findFinalString(arr, N, ch);
for ( auto itr = ans.begin();
itr != ans.end(); itr++)
cout << *itr << " " ;
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.Collections;
class GFG {
static ArrayList<Character> findFinalString( char [] arr, int n, char ch) {
ArrayList<Character> li = new ArrayList<Character>();
Boolean found = false ;
for ( int i = 0 ; i < n; i++) {
if (arr[i] == ch)
found = !found;
if (found)
li.add( 0 , arr[i]);
else
li.add(arr[i]);
}
if (found == true )
Collections.reverse(li);
return li;
}
public static void main(String args[]) {
char arr[] = { 'A' , 'B' , 'X' , 'C' , 'D' , 'X' , 'F' };
char ch = 'X' ;
int N = arr.length;
ArrayList<Character> ans = findFinalString(arr, N, ch);
for ( char itr : ans)
System.out.print(itr + " " );
}
}
|
Python3
def findFinalString(arr, n, ch):
li = []
found = 0
for i in range (n):
if arr[i] = = ch:
found = 1 - found
if found:
li.insert( 0 , arr[i])
else :
li.append(arr[i])
if found = = 1 :
li = li[:: - 1 ]
return li
if __name__ = = "__main__" :
arr = [ 'A' , 'B' , 'X' , 'C' , 'D' , 'X' , 'F' ]
ch = 'X'
N = len (arr)
ans = findFinalString(arr, N, ch)
for val in ans:
print (val, end = " " )
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
public static List< char > findFinalString( char [] arr,
int n, char ch)
{
List< char > li = new List< char >();
bool found = false ;
for ( int i = 0; i < n; i++) {
if (arr[i] == ch)
found = !found;
if (found == true )
li.Insert(0, arr[i]);
else
li.Add(arr[i]);
}
if (found == true )
li.Reverse();
return li;
}
static public void Main()
{
char [] arr = { 'A' , 'B' , 'X' , 'C' , 'D' , 'X' , 'F' };
char ch = 'X' ;
int N = arr.Length;
List< char > ans = new List< char >();
ans = findFinalString(arr, N, ch);
for ( int i = 0; i < ans.Count; i++) {
Console.Write( " " );
Console.Write(ans[i]);
}
}
}
|
Javascript
function findFinalString(arr, n, ch) {
let li = [];
let found = 0;
for (let i = 0; i < n; i++) {
if (arr[i] == ch) found = !found;
if (found) li.unshift(arr[i]);
else li.push(arr[i]);
}
if (found == 1) li.reverse();
return li;
}
let arr = [ "A" , "B" , "X" , "C" , "D" , "X" , "F" ];
let ch = "X" ;
let N = 7;
let ans = findFinalString(arr, N, ch);
console.log(ans);
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
27 Oct, 2022
Like Article
Save Article