Skip to content

Commit 79bd239

Browse files
authored
Merge pull request #3481 from Zirafnik/patch-2
Update 0058-length-of-last-word.py
2 parents e0d37ac + 2054ae5 commit 79bd239

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

python/0058-length-of-last-word.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ def lengthOfLastWord(self, s: str) -> int:
44
one shortcut
55
"""
66
# return len(s.split()[-1])
7-
c = 0
8-
for i in s[::-1]:
9-
if i == " ":
10-
if c >= 1:
11-
return c
12-
else:
13-
c += 1
14-
return c
7+
count = 0
8+
for i in range(len(s) - 1, -1, -1):
9+
char = s[i]
10+
if char == " ":
11+
if count >= 1:
12+
return count
13+
else:
14+
count += 1
15+
return count

0 commit comments

Comments
 (0)