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
- Recursive program for prime number
- Recursive program to generate power set
- C++ Program for Recursive Bubble Sort
- Recursive Program for Binary to Decimal
- Recursive program to print all subsets with given sum
- Recursive program to print formula for GCD of n integers
- Program to reverse a string (Iterative and Recursive)
- Recursive program to print all numbers less than N which consist of digits 1 or 3 only
- Program to count vowels in a string (Iterative and Recursive)
- Program to check if all characters have even frequency
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.