Skip to main content

Reverse String

EasyStringsTwo pointers303 tests

Write a function that reverses an array of characters and returns the reversed array.

Example 1

text

Example 2

text

Note this asks you to return the array, not to reverse it in place. Try it with two pointers moving toward each other rather than reaching for a built-in.

Examples

Example 1

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

The first and last characters swap, then the second and second-to-last, and so on.

Example 2

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

Case is preserved — only the order changes.

Example 3

Input:
[]
Output:
[]

An empty array reverses to an empty array.

Constraints

  • 0 <= s.length <= 10^5
  • s[i] is a printable ASCII character.

Run checks your code against the 3 examples. Submit checks it against all 303 tests and needs a free account. You can also try ideas in the free JavaScript or Python compiler.