Skip to content

Commit d567ebe

Browse files
committed
updated solution 58 to be more readable but slower
1 parent b528c6a commit d567ebe

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed
Binary file not shown.

python/questions_001_100/question_058/length_of_last_word.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,13 @@
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+
2630
length = 0
27-
isWord = False
28-
for i in range(len(s)):
29-
char = s[len(s) - 1 - i]
30-
if char == ' ':
31-
if isWord:
32-
return length
33-
else:
34-
continue
35-
else:
36-
isWord = True
37-
length += 1
31+
while s[i] != ' ':
32+
length += 1
33+
i -= 1
34+
3835
return length

0 commit comments

Comments
 (0)