This website contains ALL LeetCode **Premium** problems for
**FREE!!**.

All leaked interview problems are collected from Internet.

All leaked interview problems are collected from Internet.

Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and left the other as original.

**Example:**

Input:s = "abcdefg", k = 2Output:"bacdfeg"

- The string consists of lower English letters only.
- Length of the given string and k will in the range [1, 10000]

b'

\n

\n#### Approach #1: Direct [Accepted]

\n

\n

'
\n\n

\n\n

**Intuition and Algorithm**

We will reverse each block of `2k`

characters directly.

Each block starts at a multiple of `2k`

: for example, `0, 2k, 4k, 6k, ...`

. One thing to be careful about is we may not reverse each block if there aren\'t enough characters.

To reverse a block of characters from `i`

to `j`

, we can swap characters in positions `i++`

and `j--`

.

**Complexity Analysis**

- \n
- \n
Time Complexity: , where is the size of

\n`s`

. We build a helper array, plus reverse about half the characters in`s`

. \n - \n
Space Complexity: , the size of

\n`a`

. \n

\n

Analysis written by: @awice.

\n