344_Reverse String
[easy] [string, two pointers]
Write a function that takes a string as input and returns the string reversed.
Example 1:
Example 2:
Solution 1: two pointers
Idea:
First convert the string into a list. Loop from the beginning and from the end simultaneously. Invert the letters at symmetric places. For example, invert the first and the last letter, then second first and second last letter....
Time Complexity:
Space Complexity:
Solution 2: recursive
Idea:
For a given string, divide into two sub string with similar length, reverse the position of two substring first and then reverse letters in each substring using recursive function.
Time Complexity: depth of the recursion tree is , number of leaves in the lowest level is , thus total time complexity is
Space Complexity:
Last updated