Reverse a String Using Recursion
Reverse a string using a recursive function rather than a loop.
Define reverse(s) as: if the string is empty or one character, return it unchanged; otherwise return reverse(s without its first character) followed by that first character. Each call strips one character, and the concatenation on the way back builds the reversed string.
Problem Statement
Given a string, return it reversed, using recursion instead of an explicit loop. The idea is to express the reversal of a string in terms of the reversal of a shorter string.
For example, "hello" becomes "olleh".
Input: A string s.
Output: A new string with the characters of s in reverse order.
Examples
Input: "hello"
Output: "olleh"reverse("ello") + 'h' unwinds down to "olleh".
Input: "abc"
Output: "cba"reverse("bc") gives "cb", then + 'a' gives "cba".
Constraints
0 <= s.length() <= a few thousand (keep within the call-stack depth)Solution must be recursive
Think Before You Code
Reveal the questions to ask yourself first
- What is the smallest string you already know how to reverse without more work?
- If you could reverse everything except the first character, where does that first character go?
- How does each recursive call make the problem strictly smaller so it terminates?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Reduce the string one character at a time.
- Base case: if
s.length() <= 1, returns(an empty or single character is already reversed). - Recursive case: return
reverse(s.substring(1)) + s.charAt(0). This reverses everything after the first character, then places the first character last. - Each call shortens the string by one, so it eventually reaches the base case.
The reversed pieces are concatenated as the recursion unwinds, assembling the answer.
Dry Run
Walk through the example step by step
Reversing "abc":
call | returns
----------------+------------------------------
reverse("abc") | reverse("bc") + 'a'
reverse("bc") | reverse("c") + 'b'
reverse("c") | "c" (base case)
unwind: reverse("bc") = "c" + 'b' = "cb"
unwind: reverse("abc") = "cb" + 'a' = "cba"
Solution
Reveal the full Java solution
public class ReverseStringRecursion {
public static String reverse(String s) {
if (s.length() <= 1) {
return s;
}
return reverse(s.substring(1)) + s.charAt(0);
}
public static void main(String[] args) {
System.out.println(reverse("hello")); // olleh
System.out.println(reverse("abc")); // cba
}
}
The recursion rests on a simple identity: the reverse of a string is the reverse of everything after the first character, with that first character appended at the end. The base case (length 0 or 1) stops the descent. As the stack unwinds, each frame appends its held first character behind the already-reversed tail, producing the full reversal.
O(n^2) due to substring and concatenation at each levelSpace: O(n) call-stack depthCommon Mistakes
- Omitting the base case, so the recursion never stops and overflows the stack.
- Writing `s.charAt(0) + reverse(s.substring(1))`, which returns the original string, not the reverse.
Edge Cases to Test
- An empty string returns an empty string via the base case.
- A single character returns itself.
- Very long strings can exhaust the call stack; an iterative version avoids that.
Interview Follow-Ups
- How would you reverse the string iteratively to avoid deep recursion?
- How could you make the recursion O(n) using a char array and index parameters?
- How would you reverse only the words of a sentence recursively?
Practising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
Explore the Java Full Stack program →