Reverse a String Using Recursion

Easy ⏱ 8 min 75% acceptance ★★★★★ 4.9
Implement reverse_str(s) that returns s reversed, built up recursively (no s[::-1] slicing shortcut and no loop).

Examples

Example 1
Input
reverse_str('hello')
Output
'olleh'
Explanation

Each call strips the first char and appends it after the reversed remainder.

Example 2
Input
reverse_str('a')
Output
'a'
Explanation

Single character is the base case.

Constraints

  • s may be empty.

Topics

RecursionStrings

Companies

ZomatoSwiggy

Hints

Hint 1

Base case: len(s) <= 1, return s.

Hint 2

Recursive case: reverse_str(s[1:]) + s[0].

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.