Given a string s, our task is to move all the occurrence of letter x to the end of the string s using recursion.
Note: If there are only letter x in the given string then return the string unaltered.
Input: s= “geekxsforgexxeksxx”
All occurrence of letter ‘x’ is moved to the end.
Input: s = “xxxxx”
Since there are only letter x in the given string therefore the output is unaltered.
To solve the problem mentioned above we can use Recursion. Traverse in the string and check recursively if the current character is equal to the character ‘x’ or not. If not then print the character otherwise move to the next character until the length of the string s is reached.
Below is the implementation of the above approach:
Another Implementation involving swapping of characters:
In this approach, we will be swapping adjacent characters to bring ‘x’ at the end.
Below is the implementation of the above technique:
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.
- Count of words whose i-th letter is either (i-1)-th, i-th, or (i+1)-th letter of given word
- Move all special char to the end of the String
- Minimum move to end operations to make all strings equal
- String containing first letter of every word in a given string with spaces
- Get the first letter of each word in a string using regex in Java
- Minimum letters to be removed to make all occurrences of a given letter continuous
- Check if the string contains consecutive letters and each letter occurs exactly once
- Number of positions where a letter can be inserted such that a string becomes palindrome
- Get K-th letter of the decoded string formed by repeating substrings
- Program to accept String starting with Capital letter
- First uppercase letter in a string (Iterative and Recursive)
- Find letter's position in Alphabet using Bit operation
- Minimum number of operations to move all uppercase characters before all lower case characters
- Minimum swaps required to move all vowels occurs after consonants in a given string
- Iterative Letter Combinations of a Phone Number
- Costs required to move all 1s to each index of a given Binary Array
- Move spaces to front of string in single traversal
- Python code to move spaces to front of string in single traversal
- Program to find all Factors of a Number using recursion
- Generating all possible Subsequences using Recursion
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.