305. Recursive Reverse
A sign painter has a label written on a long strip and wants it read the other way round, so the last character must come first and the first character last. Spaces and punctuation are ordinary characters and move like every other character.
Given the string s, return a new string containing its characters in reverse order. Do it recursively: swap the outermost two characters, then ask the same question about the part strictly between them, and stop when fewer than two characters remain. The string holds at most 1000 characters.
Example 1
- Input:
- s = "tide and moon"
- Output:
- "noom dna edit"
- Explanation:
Reading the label backwards gives noom dna edit.
Example 2
- Input:
- s = "level up"
- Output:
- "pu level"
- Explanation:
The characters in reverse order are pu level.
Constraints
0 ≤ s.length ≤ 1000s consists of printable ASCII characters.
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n)
- Space
- O(n)