217. Reverse a String
Foundation1000 ms256 MBSolved by 0%
Print the given string backwards.
Recursively: the reverse of a string is its last character followed by the reverse of everything before it. The base case is the empty string, whose reverse is itself.
Do not build the answer by adding one character at a time to the front of a string in a loop. In most languages that copies the whole string each time, which turns a linear job into a quadratic one.
Constraints - `1 ≤ length of s ≤ 200000`
Input
A single line containing a string s, made of printable characters with no spaces.
Output
Print the characters of s in reverse order.
Inputhello
Outputolleh
Noteis the ordinary case
Inputa
Outputa
Noteis one character, which reverses to itself
Inputab
Outputba
Noteis the smallest string where order changes
Hint 1Approach
Hint 2Approach
Hint 3Pseudocode
Hint 4Full solution