Reverse String
EasyP1Coding
s is an array of single-character strings. Reverse it in place, using O(1) extra memory.
The iterative two-pointer swap is the expected answer. Interviewers often follow up by asking for a recursive version, which the editorial covers.
Example 1
- Input
- s = ["h","e","l","l","o"]
- Output
- ["o","l","l","e","h"]
Example 2
- Input
- s = ["H","a","n","n","a","h"]
- Output
- ["h","a","n","n","a","H"]
Example 3
- Input
- s = ["a"]
- Output
- ["a"]
Constraints
- 1 <= s.length <= 10^5
- Each element is a single printable ASCII character.
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 8. ⌘Enter runs, ⌘⇧Enter submits.