Write a function that reverses a string. The input string is given as an array of characters s.

You must do this by modifying the input array in-place with O(1) extra memory.

Example 1:

Input: s = ["h","e","l","l","o"] Output: ["o","l","l","e","h"]

Example 2:

Input: s = ["H","a","n","n","a","h"] Output: ["h","a","n","n","a","H"]

Constraints:


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

    call test_reverse()

contains

    subroutine test_reverse()
        character(len=10), parameter :: example1 = "hello"
        character(len=10), parameter :: example2 = "Hannah"
        character(len=10) :: result

        print "(A)", "Testing reverse()"
        print "(A, A)", "Example 1: ", example1
        result = reverse(example1)
        print "(A, A)", "Result:     ", result
        print "(A, L)", "Correct?:   ", result == "olleh"
        print "(/)"

        print "(A, A)", "Example 2: ", example2
        result = reverse(example2)
        print "(A, A)", "Result:     ", result
        print "(A, L)", "Correct?:   ", result == "hannah"
        print "(/)"
    end subroutine test_reverse

end program main

function reverse(s) result(r)
    implicit none
    character(len=*), intent(inout) :: s(:) !< Input string (modified in place)
    character(len=size(s)) :: r            !< Reversed string

    integer :: i, j

    ! Loop from the last element to the first one
    do i = size(s), 1, -1
        ! Swap the current element with the corresponding element in the
        ! reversed string
        j = size(s) - i + 1
        call swap(s(i), r(j))
    end do

    return
end function reverse

subroutine swap(x, y)
    implicit none
    character(len=*), intent(inout) :: x, y
    character(len=len(x)) :: tmp

    tmp = x
    x = y
    y = tmp

    return
end subroutine swap
Compiled
Executed
Correct
program reverseString

implicit none

character(len=*), parameter :: infile = "reverseString.in"
character(len=*), parameter :: outfile = "reverseString.out"

integer :: i, j, n
character(len=100000) :: s

! Read input from file
open(10, file=infile)
read(10, *) n
read(10, *) s
close(10)

! Reverse the string in-place
do i = 1, n/2
    j = n - i + 1
    s(i:i) = s(j:j)
end do

! Write output to file
open(10, file=outfile)
write(10, *) s
close(10)

end program

! Function to reverse a string in-place
function reverseString(s) result(r)

character(len=*), intent(in) :: s
character(len=len(s)) :: r

integer :: i, j

do i = 1, len(s)/2
    j = len(s) - i + 1
    r(i:i) = s(j:j)
end do

end function
🌐 Data from online sources
def reverseString(s: List[str]) -> None:
    left, right = 0, len(s) - 1
    while left < right:
        s[left], s[right] = s[right], s[left]
        left, right = left + 1, right - 1

The algorithm follows a two-pointer approach. Initialize two pointers, left and right, at the beginning and end of the input array. Using a while loop, run until the left pointer is greater than or equal to the right pointer. In the loop, swap the elements at the left and right pointers. Increment the left pointer and decrement the right pointer. Repeat this process until the loop terminates, which means the entire string has been reversed.

🌐 Data from online sources
void reverseString(vector<char>& s) {
    int left = 0, right = s.size() - 1;
    while (left < right) {
        swap(s[left++], s[right--]);
    }
}

The algorithm follows a two-pointer approach. Initialize two pointers, left and right, at the beginning and end of the input array. Using a while loop, run until the left pointer is greater than or equal to the right pointer. In the loop, swap the elements at the left and right pointers. Increment the left pointer and decrement the right pointer. Repeat this process until the loop terminates, which means the entire string has been reversed.