Conversation
CheezItMan
left a comment
There was a problem hiding this comment.
Not bad, the methods you wrote seem to work, but the time complexities are off due to misunderstanding the cost of using some of the built-in ruby methods.
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(nlogn) |
There was a problem hiding this comment.
Nope this is O(n) since n just decreases by one with each iteration.
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) |
There was a problem hiding this comment.
This is O(n^2) since s[0...-1] creates a new string of length n-1 each iteration. Both for space and time.
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def reverse_inplace(s) |
There was a problem hiding this comment.
This is not in place since you create a new string.
Think about making a helper which takes the string, a left index and right index. The base case is when left_index >= right_index Each recursive call you swap the characters at left_index or right_index and then make a recursive call with the string, and left_index + 1 and right_index -1.
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def nested(s) |
There was a problem hiding this comment.
You seem to be mixing an iterative and recursive solution, see my notes on the reverse in place method.
No description provided.