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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Least number of manipulations needed to ensure two strings have identical characters
- Reduce the string by removing K consecutive identical characters
- Check three or more consecutive identical characters or numbers
- Replace minimal number of characters to make all characters pair wise distinct
- Program to print half diamond Number-Star pattern
- Number of ways to insert two pairs of parentheses into a string of N characters
- Recursive solution to count substrings with same first and last characters
- Program to insert dashes between two adjacent odd digits in given Number
- Minimum Cost To Make Two Strings Identical
- Minimum cost to make two strings identical by deleting the digits
- Minimum Cost to make two Numeric Strings Identical
- Check whether the two Binary Search Trees are Identical or Not
- Identical Splitting in a rectangular grid
- Minimum cost to remove the spaces between characters of a String by rearranging the characters
- Program to count vowels in a string (Iterative and Recursive)
- Recursive program to print formula for GCD of n integers
- C++ Program for Recursive Bubble Sort
- Recursive program for prime number
- Program to reverse a string (Iterative and Recursive)
- Recursive program to generate power set
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.