Skip to content

Conversation

@AngelaOh
Copy link

No description provided.

Copy link

@CheezItMan CheezItMan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope this is O(n) since n just decreases by one with each iteration.


# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You seem to be mixing an iterative and recursive solution, see my notes on the reverse in place method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants