DSAverse
Initializing Sorting Algorithms...
Sorting Algorithm
Binary Tree
Graph Traversal
Preparing interactive visualizations for optimal learning experience...
Initializing Sorting Algorithms...
Preparing interactive visualizations for optimal learning experience...
Initializing Sorting Algorithms...
Preparing interactive visualizations for optimal learning experience...
Watch how recursion processes each character and builds the reversed string from the end.
Enter a string and click Start to begin the visualization
def reverse_string(s):
# Base case: empty string or single character
if len(s) <= 1:
return s
# Recursive case: reverse the rest + first character
return reverse_string(s[1:]) + s[0]
# Example usage
result = reverse_string("HELLO") # Returns "OLLEH"function reverseString(s) {
// Base case
if (s.length <= 1) {
return s;
}
// Recursive case
return reverseString(s.substring(1)) + s[0];
}
// Example usage
const result = reverseString("HELLO"); // Returns "OLLEH"Each character is processed exactly once, requiring n recursive calls for a string of length n.
The recursion depth equals the string length, creating n stack frames. Each frame stores constant data.