I just finished implementing a solution for this problem, but it’s a bit different than the recommended approach.
def reverse_words(sentence):
reversed_sentence = ""
for word in reversed(sentence.split()):
reversed_sentence += f"{word.strip()} "
return reversed_sentence.rstrip()
This solution also seems to have O(n) time and space complexity while being much simpler. Is there a reason the recommended solution seems more complicated? Is it just to illustrate the “two pointers” approach? Or am I missing some other part of the problem here?