Java Program to Reverse String Using Recursion
reverse(String str) that returns str.charAt(str.length() - 1) + reverse(str.substring(0, str.length() - 1)) until the string is empty.Examples
How to Think About It
Algorithm
Code
public class ReverseString { public static String reverse(String str) { if (str.isEmpty()) { return ""; } return str.charAt(str.length() - 1) + reverse(str.substring(0, str.length() - 1)); } public static void main(String[] args) { String input = "hello"; System.out.println(reverse(input)); } }
Dry Run
Let's trace the input "hello" through the recursive reverse method.
Initial call
reverse("hello") takes last char 'o' and calls reverse("hell")
Second call
reverse("hell") takes last char 'l' and calls reverse("hel")
Third call
reverse("hel") takes last char 'l' and calls reverse("he")
Fourth call
reverse("he") takes last char 'e' and calls reverse("h")
Fifth call
reverse("h") takes last char 'h' and calls reverse("")
Base case
reverse("") returns empty string ""
Unwinding recursion
Each call returns last char + result: 'h' + "" = "h", then 'e' + "h" = "eh", then 'l' + "eh" = "leh", then 'l' + "leh" = "lleh", then 'o' + "lleh" = "olleh"
| Call | Input String | Last Char | Recursive Call | Return Value |
|---|---|---|---|---|
| 1 | hello | o | reverse("hell") | olleh |
| 2 | hell | l | reverse("hel") | lleh |
| 3 | hel | l | reverse("he") | leh |
| 4 | he | e | reverse("h") | eh |
| 5 | h | h | reverse("") | h |
| 6 |
Why This Works
Step 1: Base Case
The recursion stops when the string is empty, returning an empty string with if (str.isEmpty()) return "";.
Step 2: Recursive Step
Each call takes the last character using str.charAt(str.length() - 1) and adds it to the reversed substring.
Step 3: Building Result
By concatenating the last character to the reversed substring, the string is reversed step by step as recursion unwinds.
Alternative Approaches
public class ReverseString { public static String reverse(String str) { return reverseHelper(str, str.length() - 1, new StringBuilder()); } private static String reverseHelper(String str, int index, StringBuilder sb) { if (index < 0) { return sb.toString(); } sb.append(str.charAt(index)); return reverseHelper(str, index - 1, sb); } public static void main(String[] args) { System.out.println(reverse("hello")); } }
public class ReverseString { public static String reverse(String str) { StringBuilder sb = new StringBuilder(); for (int i = str.length() - 1; i >= 0; i--) { sb.append(str.charAt(i)); } return sb.toString(); } public static void main(String[] args) { System.out.println(reverse("hello")); } }
Complexity: O(n) time, O(n) space
Time Complexity
The function calls itself once per character, so it runs in linear time proportional to the string length.
Space Complexity
Each recursive call adds a layer to the call stack, so space used is proportional to the string length.
Which Approach is Fastest?
The iterative approach is fastest and uses less memory, while recursion is elegant but uses more stack space.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Recursion | O(n) | O(n) | Learning recursion and small strings |
| Recursion with StringBuilder | O(n) | O(n) | Better performance with recursion |
| Iterative | O(n) | O(1) | Large strings and performance |