Tap here to switch tabs
Problem
Submissions

Problem: Reverse String II

med
30 min
Try to solve the Reverse String II problem.

Statement

Given a string s and an integer k, reverse the first k characters for every consecutive block of 2k characters in s.

If there are fewer than k characters left, reverse all of them. If there are at least k characters but fewer than 2k characters left, reverse the first k characters and leave the remaining characters as they are.

Return the resulting string.

Note: The input string contains only lowercase English letters.

Constraints:

  • 1≤1 \leq s.length ≤104\leq 10^4

  • s consists of only lowercase English letters.

  • 1≤1 \leq k ≤104\leq 10^4

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Reverse String II

med
30 min
Try to solve the Reverse String II problem.

Statement

Given a string s and an integer k, reverse the first k characters for every consecutive block of 2k characters in s.

If there are fewer than k characters left, reverse all of them. If there are at least k characters but fewer than 2k characters left, reverse the first k characters and leave the remaining characters as they are.

Return the resulting string.

Note: The input string contains only lowercase English letters.

Constraints:

  • 1≤1 \leq s.length ≤104\leq 10^4

  • s consists of only lowercase English letters.

  • 1≤1 \leq k ≤104\leq 10^4