Reverse String II
Given a string s and an integer k, reverse the first k characters for every 2k characters counting from the start of the string.
- If there are fewer than
kcharacters left, reverse all of them. - If there are less than
2kbut greater than or equal tokcharacters, reverse the firstkcharacters and leave the other characters as original.
Example 1
Input
s = "abcdefg", k = 2Output
"bacdfeg"For every 2k = 4 characters, the first 2 characters are reversed, producing "bacdfeg".
Example 2
Input
s = "abcd", k = 2Output
"bacd"The first 2 characters are reversed and the remaining characters are left unchanged.
Constraints
- 1 <= s.length <= 10^4
- s consists of only lowercase English letters.
- 1 <= k <= 10^4