695. Reverse a String Using Recursion

EasyRecursionRecursion

Given a string s, return its reverse using recursion. You must NOT use built-in reverse functions or loops. At each recursive step, move the first character to the end of the reversed remainder.

Input: A single string s (0 ≤ len(s) ≤ 1000). The string may contain letters, digits, and special characters.

Output: A single string — the reverse of s.

Examples

Example 1
Input: "hello"
Output: "olleh"
Explanation: reverseStr("hello") = reverseStr("ello") + "h" = "olle"+"h" ... = "olleh".
Example 2
Input: "a"
Output: "a"
Explanation: Single character string is already its own reverse.
Example 3
Input: "abcde"
Output: "edcba"
Explanation: reverseStr("abcde") = reverseStr("bcde")+"a" = "edcb"+"a" = "edcba".

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →