Write a function that takes in a string and an integer k
. The function should reverse every consecutive group of k
characters in the string. If the remaining characters at the end of the string are fewer than k
, simply reverse all of them.
For example, given the input string "abcdefghij"
and k = 3
, the groups are "abc"
, "def"
, "ghi"
, and "j"
. After reversing each group, the output should be "cbafedihgj"
.
Today, as we celebrate continuous learning and improvement, try not only to solve the problem but also to think about the efficiency and clarity of your code.
Your function should follow the signature below (language-specific details provided in the starter code):
// Input: string, integer k
// Output: modified string with every k characters reversed