-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_words_in_a_string.py
More file actions
48 lines (36 loc) · 1.4 KB
/
reverse_words_in_a_string.py
File metadata and controls
48 lines (36 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
151. Reverse Words in a String
Time Complexity: O(n)
Space Complexity: O(n) for the result
"""
class Solution:
def reverseWords(self, s: str) -> str:
words = s.split()
return ' '.join(reversed(words))
# Test cases
if __name__ == "__main__":
solution = Solution()
# Example 1
s1 = "the sky is blue"
print(f"Example 1: '{solution.reverseWords(s1)}'") # Expected: "blue is sky the"
# Example 2
s2 = " hello world "
print(f"Example 2: '{solution.reverseWords(s2)}'") # Expected: "world hello"
# Example 3
s3 = "a good example"
print(f"Example 3: '{solution.reverseWords(s3)}'") # Expected: "example good a"
# Edge case: single word
s4 = "hello"
print(f"Example 4: '{solution.reverseWords(s4)}'") # Expected: "hello"
# Edge case: two words
s5 = "hello world"
print(f"Example 5: '{solution.reverseWords(s5)}'") # Expected: "world hello"
# Edge case: lots of spaces
s6 = " a b c "
print(f"Example 6: '{solution.reverseWords(s6)}'") # Expected: "c b a"
# Test manual approach
s7 = "the sky is blue"
print(f"Example 7 (manual): '{solution.reverseWordsManual(s7)}'") # Expected: "blue is sky the"
# Test two-reversal approach
s8 = "a good example"
print(f"Example 8 (two-reversal): '{solution.reverseWordsTwoReversals(s8)}'") # Expected: "example good a"