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

Anibel - Reverse Sentence - Edges #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

anibelamerica
Copy link

The time complexity for this reverse_sentence implementation is linear or O(n), where n equals the length of the array. The time complexity is linear since it it using a combination of 2 methods in succession, both of which run in O(n) time. The first method, reverses the words in the sentence (time complexity of O(n), space complexity of O(1)) and the second method reverses the entire string (time complexity of O(n), space complexity of O(1)). Since both methods are used in succession, the time complexity is O(2*n) but since we drop constants, it is O(n).

The space complexity of the method is constant of O(1) for similar reasons as above. The helper methods both allocate space to hold 3 integers each, regardless of input size. Thus, the reverse_sentence method has a constant space complexity as it uses up space in the cache for 6 integers regardless of input size.

@shrutivanw
Copy link

Nice work! 👍

Good job explaining the time and space complexity.

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