-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reverse words in a string
41 lines (32 loc) · 1 KB
/
Reverse words in a string
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
import re
class Solution(object):
def reverseWords(self, s):
"""
:type s: str
:rtype: str
"""
# s = s.strip()
# s = re.sub(r' +', ' ', s)
# print(s)
# s_list = s.split()
# return ' '.join(s.split()[::-1])
chars = [t for t in s]
slow, n = 0, len(chars)
for fast in range(n):
if chars[fast]!=" " or (fast>0 and chars[fast]==" " and chars[fast-1]!=" "):
chars[slow] = chars[fast]
slow += 1
if slow==0:
return ""
if chars[-1]==" ":
chars = chars[:slow-1]
else:
chars = chars[:slow]
chars.reverse()
# print(chars)
l, r = 0, len(chars)
for p in range(r+1):
if p==r or chars[p]==" ":
chars[l:p] = chars[l:p][::-1]
l = p+1
return "".join(chars)