We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4d28c88 commit d0bfef2Copy full SHA for d0bfef2
src/main/java/com/thealgorithms/strings/ReverseString.java
@@ -86,4 +86,19 @@ public static String reverseStringUsingStack(String str) {
86
}
87
return reversedString.toString();
88
89
+
90
+ /**
91
+ * Reverse a string using recursion
92
+ *
93
+ * @param str The input string to reverse
94
+ * @return Reversed string
95
+ */
96
+ public static String reverseRecursive(String str) {
97
+ // Base case: if string is empty or single character
98
+ if (str == null || str.length() <= 1) {
99
+ return str;
100
+ }
101
+ // Recursive step: last char + reverse of rest of string
102
+ return str.charAt(str.length() - 1) + reverseRecursive(str.substring(0, str.length() - 1));
103
104
0 commit comments