From fa8bbdeecbfbd0356fd18dfe85f5a2fe80bb73de Mon Sep 17 00:00:00 2001 From: ktkovachev <143198904+ktkovachev@users.noreply.github.com> Date: Mon, 8 Apr 2024 16:29:49 +0100 Subject: [PATCH] Solve 58. Length of Last Word --- python/58_length_of_last_word.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 python/58_length_of_last_word.py diff --git a/python/58_length_of_last_word.py b/python/58_length_of_last_word.py new file mode 100644 index 0000000..fcaee0b --- /dev/null +++ b/python/58_length_of_last_word.py @@ -0,0 +1,9 @@ +class Solution: + def lengthOfLastWord(self, s: str) -> int: + end = len(s)-1 + while end >= 0 and s[end] == " ": + end -= 1 + i = end + while i >= 0 and s[i] != " ": + i -= 1 + return end-i