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

Layla - Edges - reverse sentence #34

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

Conversation

lbristol88
Copy link

The time complexity is O(n) because the while loops are not nested loops. The space complexity if O(n) because no additional strings are being created since the algorithm reverses the sentence in place.


return
end

def reverse_sentence(my_sentence)

Choose a reason for hiding this comment

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

This method only allocates two temporary storage spaces to hold the start_point and end_point. This is constant since it does not change based on the number of characters in the input string, my_sentence.

So, the time and space complexity of this method is simply the sum of the time and space complexities of the two methods it calls one after another.

return
end

def reverse_word(my_words, start_point, end_point)

Choose a reason for hiding this comment

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

This method uses three temporary storage values: two to hold the index values i and j and one to hold the temporary character while swapping. So, regardless of how many characters are in the input or what characters are in the string, this method will always need 3 temporary storage values. Hence, the space complexity is O(1) or constant.

To deduce the time complexity, we count the number of operations in this function as compared to either the input value or input size. In this case, it would depend on the number of characters from start_point to end_point If there are n characters between start_point and end_point then the while loop will run n/2 times or in other words the method will swap characters n/2 times. That's why, the time complexity will be O(n/2) or by dropping the constant, O(n) or linear.

@@ -1,4 +1,52 @@
# A method to reverse the words in a sentence, in place.
# A method to reverse each word in a sentence, in place.
def reverse_words(my_words)

Choose a reason for hiding this comment

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

This method uses four temporary storage spaces to hold position, string_length, start_point and end_point. This is always true regardless of the input my_words (the value of it or the number of characters in it). Hence, the space complexity of this method is O(1) or constant.

To count the number of operations, we look at the nested loops. By going through one of the two inner, until loops, all n characters in my_words are examined and compared. Through the examination the beginning and end indices of a word is identified. Then, reverse_word is called - which reverses the characters in the word by examining the characters in the word one more time. Imagine a scenario where there is only one word in the input string. In this case, all n characters will be examined once to determine that the word begins at index 0 and ends at index string_length-1. Then, reverse_word reverses the characters in the word by swapping n/2 times. In this case there are n+(n/2) operations. Let's consider the other extreme, where there are only spaces in the string. In this case, all n characters will be examined once. reverse_word will not conduct any swaps since start_point will be greater than end_point. So, regardless, the number of operations will be O(n) or linear.

@shrutivanw
Copy link

Nice work, Layla! The algorithm and code looks good.

The time complexity of your algorithm is indeed O(n). However, the space complexity is constant or O(1). I've added comments inline to share the explanation for this. Slack me to share if you have any follow up questions.

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