-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse_word_in_string.py
40 lines (33 loc) · 1.09 KB
/
reverse_word_in_string.py
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
class Solution:
"""
Task:
Given an input string s, reverse the order of the words.
A word is defined as a sequence of non-space characters.
The words in s will be separated by at least one space.
Return a string of the words in reverse order concatenated by a single space.
Note that s may contain leading or trailing spaces or multiple spaces between two words.
The returned string should only have a single space separating the words.
Do not include any extra spaces.
"""
# Method 1
def reverseWords(self, s: str) -> str:
reversed = ""
s = s.split()
i = 0
j = len(s)-1
while i < len(s)//2 and i < j:
s[i], s[j] = s[j], s[i]
i += 1
j -= 1
for i in range(len(s)):
reversed += s[i]
if i < len(s)-1:
reversed += " "
return reversed
# Method 2
def reverseWords(self, s: str) -> str:
s = s.split()
n = len(s)
for i in range(n//2):
s[i], s[n-i-1] = s[n-i-1], s[i]
return ' '.join(s)