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 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 leave the other as original.

Example 1:

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

Example 2:

Input: s = "abcd", k = 2 Output: "bacd"

Constraints:

  • 1 <= s.length <= 104
  • s consists of only lowercase English letters.
  • 1 <= k <= 104

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    character(len=:), allocatable :: s
    integer :: k

    call get_string(s)
    read(*,*) k

    call solve(s, k)

contains

subroutine get_string(str)
    implicit none
    character(len=:), allocatable :: str
    integer :: i, n

    read(*,*) n
    allocate(character(n) :: str)
    do i = 1, n
        read(*,*) str(i:i)
    end do
end subroutine get_string

subroutine solve(s, k)
    implicit none
    character(len=*), intent(inout) :: s
    integer, value :: k
    integer :: i, j, n

    n = len(s)
    do i = 1, n, 2*k
        if (i+k-1 > n) then
            ! Reverse all remaining characters
            do j = i, n
                s(j:j) = s(j:j)
Compiled
Executed
Correct
module reverse_string

implicit none

contains

subroutine reverse_string(s, k)

character(len=*), intent(inout) :: s
integer, intent(in) :: k

integer :: i, j, n

n = len(s)

do i = 1, n, k
    j = min(i + k - 1, n)
    s(i:j) = s(i:j)(reverse(s(i:j)))
end do

end subroutine reverse_string

end module reverse_string

program main

use reverse_string

implicit none

character(len=:), allocatable :: s
integer :: k

s = "abcdefg"
k = 2
call reverse_string(s, k)
write (*,*) s

s = "abcd"
k = 2
call reverse_string(s, k)
write (*,*) s

end program main
🌐 Data from online sources
def reverseStr(s, k):
    chars = list(s)
    for i in range(0, len(chars), 2 * k):
        chars[i:i + k] = reversed(chars[i:i + k])
    return "".join(chars)

The algorithm first creates a mutable array from the original string. Then, we iterate over every 2k characters. If there are at least k characters remaining from the current position, we reverse the first k characters. Otherwise, we reverse all the remaining characters.

In C++, we use the reverse function that takes iterators as arguments. In Java, JavaScript, and Python, we use custom loops for reversing k characters, taking into account the case when there are fewer than k characters remaining. Then, we join the characters back into a string.

🌐 Data from online sources
#include <algorithm>
#include <string>
using namespace std;

string reverseStr(string s, int k) {
    for (int i = 0; i < s.size(); i += 2*k) {
        if (i + k <= s.size()) {
            reverse(s.begin() + i, s.begin() + i + k);
        } else {
            reverse(s.begin() + i, s.end());
        }
    }
    return s;
}

The algorithm first creates a mutable array from the original string. Then, we iterate over every 2k characters. If there are at least k characters remaining from the current position, we reverse the first k characters. Otherwise, we reverse all the remaining characters.

In C++, we use the reverse function that takes iterators as arguments. In Java, JavaScript, and Python, we use custom loops for reversing k characters, taking into account the case when there are fewer than k characters remaining. Then, we join the characters back into a string.