Problem description

You are given a string represented as an array of individual characters. Rearrange the characters so that the sequence reads from back to front, effectively flipping the order of every element.

The transformation must happen directly inside the given array. You are not allowed to allocate a second array to hold the result, so the work should be done using only a constant amount of extra space.

Example 1

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

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

Each character trades place with its mirror position from the opposite end.

Example 2

Input: s = ["A","d","m","i","t"]

Output: ["t","i","m","d","A"]

The first and last characters swap, then the pair moving inward.

Constraints

  • 1 <= s.length <= 10^5
  • s[i] is a printable ASCII character
  • You must modify the array in place with O(1) extra memory

After the pattern

Now solve one you haven't seen.

Flip the Stamp Strip is our own problem on the technique behind Reverse String: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.

1,194 problems · 15,062 graded tests · free