Reverse String
You are given a string s. Return the string with its characters in reverse order. The classic interview answer converts the string to a character array and…
You are given a string `s`. Return the string with its characters in reverse order. The classic interview answer converts the string to a character array and reverses it in place using two pointers that converge from both ends, swapping pairs until they meet. For example, reversing `"hello"` yields `"olleh"`.
- `0 <= s.length <= 1000` - `s` may contain letters, digits, spaces, and printable ASCII characters. - The interactive visualizer accepts at most 40 characters; longer valid judge inputs are execution-only. - The reversal must preserve every character — only their order changes. - The string-returning contract uses O(n) space for a mutable character copy. - If the input is already a mutable character array, mirrored swaps use O(1) auxiliary space. - Output is the string read from last character to first.
Example 1: input s = "hello"; output "olleh". Swap (h, o), then swap (e, l). The middle character l stays in place, giving "olleh".
Example 2: input s = "ab cd"; output "dc ba". Characters — including the space — are reversed by position: "ab cd" becomes "dc ba".
Example 3: input s = "x"; output "x". A single character reads the same in either direction; nothing is swapped.
Category: Strings. Topic: strings. Difficulty: Easy. Patterns: two-pointers.