Skip to content

Commit 2fd9b0b

Browse files
committed
Time: 34 ms (74.62%), Space: 16.8 MB (17.17%) - LeetHub
1 parent c388bb1 commit 2fd9b0b

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def reverseWords(self, s: str) -> str:
3+
# Step 1: Trim the string to remove leading and trailing spaces
4+
s = s.strip()
5+
6+
# Step 2: Split the string into words based on spaces
7+
words = s.split()
8+
9+
# Step 3: Reverse the list of words
10+
words.reverse()
11+
12+
# Step 4: Join the reversed list of words with a single space between them
13+
reversed_string = ' '.join(words)
14+
15+
return reversed_string

0 commit comments

Comments
 (0)