Reverse Words in a String
Solution Code
class Solution:
def reverseWords(self, s: str) -> str:
words = []
current_word = []
for char in s:
if char != ' ':
current_word.append(char)
elif current_word:
words.append("".join(current_word))
current_word = []
if current_word:
words.append("".join(current_word))
return " ".join(words[::-1])Time Complexity
O(N)
Space Complexity
O(N)

