EasyStringsTwo Pointers

Reverse String

LeetCode
1 approach, code in all languages

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
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now