Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amanda Ungco- Reverse Words #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

amandaungco
Copy link

Time Complexity: O(n^2)
space complexity: O(n)

while string[i] != " " && i < string.length
i += 1
end
space = i

Choose a reason for hiding this comment

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

Minor: you could simply return i and don't need to create another variable called space in this method.

j = 0
else j = space - 1
end
my_words[i..j] = string_reverse(my_words[i..j])

Choose a reason for hiding this comment

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

As we saw in class, in the first few slides of https://drive.google.com/file/d/0B__DV26QHsH4bWJmS1A0QXBad1U/view?usp=sharing, the substring method e.g. my_words[i..j] creates a new string object. As a result, you will end up using more space and your string manipulations are not happening in-place.

Slack me if you have further questions on this.

@shrutivanw
Copy link

Although your string_reverse method is doing the string manipulation in-place, your reverse_words method is creating new string objects and manipulating those.

Take a look at my solution at https://github.com/Ada-C10/reverse_words/blob/solution/lib/reverse_words.rb

Do slack me if you have questions on any of this, and if needed, we could schedule a video call to discuss further.

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