Move all occurrence of letter ‘x’ from the string s to the end using Recursion
Last Updated :
19 Sep, 2022
Given a string s, our task is to move all the occurrence of letter x to the end of the string s using recursion.
Note: If there are only letter x in the given string then return the string unaltered.
Examples:
Input: s= “geekxsforgexxeksxx”
Output: geeksforgeeksxxxxx
Explanation:
All occurrence of letter ‘x’ is moved to the end.
Input: s = “xxxxx”
Output: xxxxx
Explanation:
Since there are only letter x in the given string therefore the output is unaltered.
Approach:
To solve the problem mentioned above we can use Recursion. Traverse in the string and check recursively if the current character is equal to the character ‘x’ or not. If not then print the character otherwise move to the next character until the length of the string s is reached.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void moveAtEnd(string s, int i, int l)
{
if (i >= l)
return ;
char curr = s[i];
if (curr != 'x' )
cout << curr;
moveAtEnd(s, i + 1, l);
if (curr == 'x' )
cout << curr;
return ;
}
int main()
{
string s = "geekxsforgexxeksxx" ;
int l = s.length();
moveAtEnd(s, 0, l);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void moveAtEnd(String s, int i, int l)
{
if (i >= l)
return ;
char curr = s.charAt(i);
if (curr != 'x' )
System.out.print(curr);
moveAtEnd(s, i + 1 , l);
if (curr == 'x' )
System.out.print(curr);
return ;
}
public static void main(String args[])
{
String s = "geekxsforgexxeksxx" ;
int l = s.length();
moveAtEnd(s, 0 , l);
}
}
|
Python3
def moveAtEnd(s, i, l):
if (i > = l):
return
curr = s[i]
if (curr ! = 'x' ):
print (curr, end = "")
moveAtEnd(s, i + 1 , l)
if (curr = = 'x' ):
print (curr, end = "")
return
if __name__ = = '__main__' :
s = "geekxsforgexxeksxx"
l = len (s)
moveAtEnd(s, 0 , l)
|
C#
using System;
class GFG{
static void moveAtEnd( string s, int i, int l)
{
if (i >= l)
return ;
char curr = s[i];
if (curr != 'x' )
Console.Write(curr);
moveAtEnd(s, i + 1, l);
if (curr == 'x' )
Console.Write(curr);
return ;
}
public static void Main()
{
string s = "geekxsforgexxeksxx" ;
int l = s.Length;
moveAtEnd(s, 0, l);
}
}
|
Javascript
<script>
function moveAtEnd(s, i, l)
{
if (i >= l)
return ;
let curr = s[i];
if (curr != 'x' )
document.write(curr);
moveAtEnd(s, i + 1, l);
if (curr == 'x' )
document.write(curr);
return ;
}
let s = "geekxsforgexxeksxx" ;
let l = s.length;
moveAtEnd(s, 0, l);
</script>
|
Output
geeksforgeeksxxxxx
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(n) for call stack
Another Implementation involving swapping of characters:
In this approach, we will be swapping adjacent characters to bring ‘x’ at the end.
Below is the implementation of the above technique:
C++
#include<bits/stdc++.h>
using namespace std;
void rec( char *a, int i)
{
if (i == 0)
{
cout << a << endl;
return ;
}
if (a[i] == 'x' )
{
int j = i;
while (a[j] != '\0' && a[j+1] != '\0' )
{
swap(a[j], a[j+1]);
j++;
}
}
rec(a, i - 1);
}
int main()
{
char a[] = { 'g' , 'e' , 'e' , 'k' , 'x' ,
's' , 'x' , 'x' , 'k' , 's' , '\0' };
int n = 10;
rec(a,n-1);
}
|
Java
import java.util.*;
class Main{
public static void rec( char a[],
int i)
{
if (i == 0 )
{
System.out.println(a);
return ;
}
if (a[i] == 'x' )
{
int j = i;
while (a[j] != '\0' &&
a[j + 1 ] != '\0' )
{
char temp = a[j];
a[j] = a[j + 1 ];
a[j + 1 ] = temp;
j++;
}
}
rec(a, i - 1 );
}
public static void main(String[] args)
{
char a[] = { 'g' , 'e' , 'e' , 'k' ,
'x' , 's' , 'x' , 'x' ,
'k' , 's' , '\0' };
int n = 10 ;
rec(a,n- 1 );
}
}
|
Python3
def rec(a, i):
if (i = = 0 ):
a.pop()
print ("".join(a))
return
if (a[i] = = 'x' ):
j = i
while (a[j] ! = '\0' and
a[j + 1 ] ! = '\0' ):
(a[j], a[j + 1 ]) = (a[j + 1 ], a[j])
j + = 1
rec(a, i - 1 )
if __name__ = = "__main__" :
a = [ 'g' , 'e' , 'e' , 'k' , 'x' ,
's' , 'x' , 'x' , 'k' , 's' , '\0' ]
n = 10
rec(a, n - 1 )
|
C#
using System;
class GFG
{
static void rec( char [] a, int i)
{
if (i == 0)
{
Console.WriteLine(a);
return ;
}
if (a[i] == 'x' )
{
int j = i;
while (a[j] != '\0' &&
a[j + 1] != '\0' )
{
char temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
j++;
}
}
rec(a, i - 1);
}
static void Main()
{
char [] a = { 'g' , 'e' , 'e' , 'k' ,
'x' , 's' , 'x' , 'x' ,
'k' , 's' , '\0' };
int n = 10;
rec(a,n-1);
}
}
|
Javascript
<script>
function rec(a, i)
{
if (i == 0)
{
document.write(a.join( "" ));
return ;
}
if (a[i] == 'x' )
{
let j = i;
while (a[j] != '\0' &&
a[j + 1] != '\0' )
{
let temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
j++;
}
}
rec(a, i - 1);
}
let a = [ 'g' , 'e' , 'e' , 'k' , 'x' , 's' , 'x' , 'x' , 'k' , 's' , '\0' ];
let n = 10;
rec(a, n - 1);
</script>
|
Time Complexity: O(N), where N is the length of the given string.
Auxiliary Space: O(N).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...