Reverse String
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.

30:00

Reverse String
easy
Topics
Companies

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: ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
Constraints:
  • 1s.length1051 \leq s.\text{length} \leq 10^5

  • s[i] is a printable ASCII character.

Input
arr =["h","e","l","l","o"]

Initialize two pointers: left=0, right=4

Character Array
'h'
'e'
'l'
'l'
'o'
0
1
2
3
4

left = 0

right = 4

LRswap

Two Pointer Approach:

  1. Initialize left=0, right=n-1
  2. While left < right: swap s[left] and s[right]
  3. Move left++, right--
Left Pointer
Right Pointer
Swapped
Variables
No variables to display
DepthFunction Call
Stack empty
0/4