Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/main/java/com/thealgorithms/strings/ReverseString.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,19 @@ public static String reverseStringUsingStack(String str) {
}
return reversedString.toString();
}

/**
* Reverse a string using recursion
*
* @param str The input string to reverse
* @return Reversed string
*/
public static String reverseRecursive(String str) {
// Base case: if string is empty or single character
if (str == null || str.length() <= 1) {
return str;
}
// Recursive step: last char + reverse of rest of string
return str.charAt(str.length() - 1) + reverseRecursive(str.substring(0, str.length() - 1));
}
}
Loading