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:
1 <= s.length <= 105
s[i]
is a printable ascii character.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
temp.f95:15:24: 15 | result = reverse(example1) | 1 Error: Return type mismatch of function ‘reverse’ at (1) (UNKNOWN/CHARACTER(0)) temp.f95:15:17: 15 | result = reverse(example1) | 1 Error: Function ‘reverse’ at (1) has no IMPLICIT type temp.f95:15:24: 15 | result = reverse(example1) | 1 Error: Return type mismatch of function ‘reverse’ at (1) (UNKNOWN/CHARACTER(0)) temp.f95:21:17: 21 | result = reverse(example2) | 1 Error: Function ‘reverse’ at (1) has no IMPLICIT type temp.f95:41:24: 41 | call swap(s(i), r(j)) | 1 Error: Non-variable expression in variable definition context (actual argument to INTENT = OUT/INOUT) at (1) temp.f95:15:25: 15 | result = reverse(example1) | 1 Error: Rank mismatch in argument ‘s’ at (1) (rank-1 and scalar) temp.f95:21:25: 21 | result = reverse(example2) | 1 Error: Rank mismatch in argument ‘s’ at (1) (rank-1 and scalar)
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
temp.f95:31:0: 1 | program reverseString | 2 ...... 31 | function reverseString(s) result(r) | Error: Global name ‘reversestring’ at (1) is already being used as a PROGRAM at (2)
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.
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.