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
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)
f951: Error: Unexpected end of file in ‘temp.f95’
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
temp.f95:7:25: 7 | subroutine reverse_string(s, k) | 1 Error: MODULE attribute of ‘reverse_string’ conflicts with PROCEDURE attribute at (1) temp.f95:9:36: 9 | character(len=*), intent(inout) :: s | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:24: 10 | integer, intent(in) :: k | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:12:18: 12 | integer :: i, j, n | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:14:10: 14 | n = len(s) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:16:14: 16 | do i = 1, n, k | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:17:25: 17 | j = min(i + k - 1, n) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:19:3: 19 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:21:3: 21 | end subroutine reverse_string | 1 Error: Expecting END MODULE statement at (1) temp.f95:27:5: 27 | use reverse_string | 1 Fatal Error: Cannot open module file ‘reverse_string.mod’ for reading at (1): No such file or directory compilation terminated.
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.
#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.