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 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:
- C
Output:
ge*eks
Note:The recursive function in the above code is tail recursive as recursive call is the last thing executed by the function.
Comments
Post a Comment