-
Notifications
You must be signed in to change notification settings - Fork 4
/
Reverse-Words-In-A-String.py
42 lines (42 loc) · 1.06 KB
/
Reverse-Words-In-A-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
41
42
class Solution:
def reverseWords(self, s: str) -> str:
s = list(s)
n = len(s)
self.Reverse(s,0,n-1)
i =0
while i<n:
while i<n and s[i]==' ':
i+=1
start = end = i
while i<n and s[i]!= ' ':
i+=1
end+=1
self.Reverse(s,start,end-1)
s = self.cleanSpaces(s)
return s
def Reverse(self,s,start,end):
while start<end:
s[start],s[end] = s[end],s[start]
start+=1
end-=1
def cleanSpaces(self,s):
n = len(s)
i=0
j=0
while j<n:
while j<n and s[j]==' ':
j+=1
while j<n and s[j]!=' ':
s[i]=s[j]
i+=1
j+=1
while j<n and s[j]==' ':
j+=1
if j<n:
s[i] = ' '
i+=1
return "".join(s[:i])
### Solution 2:
class Solution:
def reverseWords(self, s: str) -> str:
return " ".join(s.split()[::-1])