Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The time complexity for this
reverse_sentence
implementation is linear orO(n)
, wheren
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 inO(n)
time. The first method, reverses the words in the sentence (time complexity ofO(n)
, space complexity ofO(1)
) and the second method reverses the entire string (time complexity ofO(n)
, space complexity ofO(1)
). Since both methods are used in succession, the time complexity isO(2*n)
but since we drop constants, it isO(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, thereverse_sentence
method has a constant space complexity as it uses up space in the cache for 6 integers regardless of input size.