Given a string with repeated characters, we have to insert a star i.e.” * “ between pair of adjacent identical characters using recursion.
Input : aabb Output : a*ab*b Input : xxxy Output : x*x*xy
- 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 first two positions of the string so we now make a recursive call without the first character of the string.
Else we do the following-
The above approach has been implemented below:
Note:The recursive function in the above code is tail recursive as recursive call is the last thing executed by the function.
- Replace minimal number of characters to make all characters pair wise distinct
- Reduce the string by removing K consecutive identical characters
- Least number of manipulations needed to ensure two strings have identical characters
- Recursive solution to count substrings with same first and last characters
- Program to print half diamond Number-Star pattern
- C++ Program for Recursive Bubble Sort
- Recursive program to generate power set
- Recursive Program for Binary to Decimal
- Recursive program for prime number
- Recursive program to print all subsets with given sum
- Recursive program to print formula for GCD of n integers
- Recursive program to find all Indices of a Number
- Recursive program to replace all occurrences of pi with 3.14 in a given string
- Program to reverse a string (Iterative and Recursive)
- Program to count vowels in a string (Iterative and Recursive)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.