Reverse String In Place
Problem statement
Reverse the character array chars in place and return that same array.
Use O(1) auxiliary space. Do not build another array or string whose size grows with the input. The returned array is the mutated input; output serialization does not count as auxiliary space.
Function
reverseString(chars: char[]) → char[]Examples
Example 1
chars = ["h","e","l","l","o"]return = ["o","l","l","e","h"]Swap h with o, then e with the second l. The middle l stays in place, giving olleh.
Example 2
chars = ["a","b"]return = ["b","a"]One swap exchanges the two characters, giving ba.
Constraints
0 <= chars.length <= 2000- Each element is one printable ASCII character, with code from
32through126.