Reverse a String In-Place
Implement reverseStringInPlace
Given a string
s, return it reversed.
The naive approach rebuilds the result one character at a time through string concatenation — correct, but wasteful in languages where strings are immutable, since every append can re-copy everything built so far. The two pointerTwo PointerUsing two indices that move toward (or away from) each other through a structure, instead of a single pass that only moves in one direction. technique avoids that entirely: convert the string to a character array and swap from both ends inward, using no more than a single temporary variable at any moment.
Example 1:
Input: s = "hello"
Output: "olleh"
Example 2:
Input: s = "a"
Output: "a"
Example 3:
Input: s = "ab"
Output: "ba"
+ 7 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ s.length ≤ 10⁵ - ●
s consists of printable ASCII characters
s =
hello