Given a string str and an integer k, the task is to reverse alternate k characters of the given string. If characters present are less than k, leave them as it is.
Input: str = “geeksforgeeks”, k = 3
Input: str = “abcde”, k = 2
Approach: The idea is to first reverse k characters, then jump onto the next k characters by adding 2 * k to the index and so on until the complete string is modified.
Below is the implementation of the above approach:
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Minimum number of characters to be removed to make a binary string alternate
- Add index to characters and reverse the string
- Reverse alternate levels of a perfect binary tree
- Given a linked list, reverse alternate nodes and append at the end
- Reverse alternate K nodes in a Singly Linked List
- Reverse alternate levels of a perfect binary tree using Stack
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Reverse Middle X Characters
- Min flips of continuous characters to make all characters same in a string
- Reverse an array without affecting special characters
- String with k distinct characters and no same characters adjacent
- Alternate vowel and consonant string
- Lexicographically first alternate vowel and consonant string
- Check if it is possible to rearrange a binary string with alternate 0s and 1s
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.
Improved By : mohit kumar 29