Recursive program to insert a star between pair of identical characters
Last Updated :
13 Dec, 2022
Given a string with repeated characters, we have to insert a star i.e.” * “ between pair of adjacent identical characters using recursion.
Examples:
Input : aabb
Output : a*ab*b
Input : xxxy
Output : x*x*xy
Approach:
- If there is an empty string then simply return. This forms our base condition.
- Check if the first two characters are identical. If yes, then insert ” * ” between them.
- As we have now checked for identical characters at the first two positions of the string so we now make a recursive call without the first character of the string.
The above approach has been implemented below:
C++
#include <iostream>
using namespace std;
void pairStar(string& input, string& output,
int i = 0)
{
output = output + input[i];
if (i == input.length() - 1)
return ;
if (input[i] == input[i + 1])
output = output + '*' ;
pairStar(input, output, i+1);
}
int main()
{
string input = "geeks" , output = "" ;
pairStar(input, output);
cout << output << endl;
return 0;
}
|
Java
class GFG
{
static String output= "" ;
static void pairStar(String input,
int i)
{
output = output + input.charAt(i);
if (i == input.length() - 1 )
return ;
if (input.charAt(i) == input.charAt(i+ 1 ))
output = output + '*' ;
pairStar(input, i+ 1 );
}
public static void main(String[] args)
{
String input = "geeks" ;
pairStar(input, 0 );
System.out.println(output);
}
}
|
Python3
def pairStar( Input , Output, i = 0 ) :
Output = Output + Input [i]
if (i = = len ( Input ) - 1 ) :
print (Output)
return ;
if ( Input [i] = = Input [i + 1 ]) :
Output = Output + '*' ;
pairStar( Input , Output, i + 1 );
if __name__ = = "__main__" :
Input = "geeks"
Output = ""
pairStar( Input , Output);
|
C#
using System;
class GFG
{
static String output= "" ;
static void pairStar(String input,
int i)
{
output = output + input[i];
if (i == input.Length - 1)
return ;
if (input[i] == input[i+1])
output = output + '*' ;
pairStar(input, i+1);
}
public static void Main(String[] args)
{
String input = "geeks" ;
pairStar(input,0);
Console.WriteLine(output);
}
}
|
PHP
<?php
function pairStar(& $input , & $output , $i = 0)
{
$output = $output . $input [ $i ];
if ( $i == strlen ( $input ) - 1)
return ;
if ( $input [ $i ] == $input [ $i + 1])
$output = $output . '*' ;
pairStar( $input , $output , $i +1);
}
$input = "geeks" ;
$output = "" ;
pairStar( $input , $output );
echo $output ;
return 0;
?>
|
Javascript
<script>
let output= "" ;
function pairStar(input,i)
{
output = output + input[i];
if (i == input.length - 1)
return ;
if (input[i] == input[i+1])
output = output + '*' ;
pairStar(input, i+1);
}
let input = "geeks" ;
pairStar(input,0);
document.write(output);
</script>
|
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(n), where n is the length of the given string.
Note: The recursive function in the above code is tail recursive as the recursive call is the last thing executed by the function.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...