Skip to content

Commit e300477

Browse files
committed
Init
1 parent d60f1ce commit e300477

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Python/Reverse_Words_In_String_III.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#Question: Given a string, reverse each word in the string
2+
#Solution: Reverse each word in the string using a helper function
3+
#Difficulty: Easy
4+
5+
def reverseWords(self, s):
6+
"""
7+
:type s: str
8+
:rtype: str
9+
"""
10+
s = list(s)
11+
def rev(s, l, r):
12+
while l < r:
13+
s[l], s[r] = s[r], s[l]
14+
r -= 1
15+
l += 1
16+
l, r = 0, 0
17+
while r < len(s):
18+
r += 1
19+
if r == len(s) or s[r] == " ":
20+
rev(s, l, r - 1)
21+
l = r + 1
22+
r += 1
23+
return "".join(s)

0 commit comments

Comments
 (0)