Reverse a String with Two Pointers
Problem statement
Given a string s, return its characters in reverse order. Preserve every character exactly, including spaces, punctuation, and letter case.
Use two pointers that move inward from opposite ends. You may copy the input to a mutable character array, swap the two pointed-to characters, and return the resulting string. The empty string returns the empty string.
Function
reverseString(s: String) → StringExamples
Example 1
s = "hello"return = "olleh"Swap h with o and e with l; the middle l stays in place.
Example 2
s = "A b!"return = "!b A"Spaces and punctuation reverse just like letters; case is unchanged.
Constraints
0 <= s.length <= 10000.- Every character is printable ASCII, with character code from
32through126.