Best Problems on Recursion | Recursion in C++
Recursion - I Print String in Reverse Objective: To print the string in reverse. Base Case: If the string is empty: return; Reverse print the remaining string using recursion, and print then print the current character. Time Complexity: O(N2) [ IMP] Space Complexity: O(N2) [ IMP] Time complexity will be O(N2) because s.substr(i) takes O(N) times and it is called O(N) times. Space complexity will be O(N2) because s.substr(i) gives a string of O(N) size and it is called O(N) times. FollowUp: Try to do this in O(N) time and space. Hint: Pass by reference and indices. Move all ‘x’ to the end of the string If the string is empty: return “ “; If the current character ch is ‘x’, we add the resultant string + ch, Else ...