Skip to content

Commit 60200c5

Browse files
committed
updated solution 58 to leverage built-in Python functions and be more concise
1 parent d567ebe commit 60200c5

File tree

2 files changed

+1
-10
lines changed

2 files changed

+1
-10
lines changed
Binary file not shown.

python/questions_001_100/question_058/length_of_last_word.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,4 @@
2323

2424
class Solution:
2525
def length_of_last_word(self, s: str) -> int:
26-
i = len(s) - 1
27-
while s[i] == ' ':
28-
i -= 1
29-
30-
length = 0
31-
while s[i] != ' ':
32-
length += 1
33-
i -= 1
34-
35-
return length
26+
return 0 if not s else len(s.rstrip().split(' ')[-1])

0 commit comments

Comments
 (0)